Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Returning a...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 16 Topic 45864 of 48020
Post > Topic >>

Re: Returning a Struct from a Function

by peter koch <peter.koch.larsen@[EMAIL PROTECTED] > May 11, 2008 at 11:19 AM

On 11 Maj, 19:57, alex.j...@[EMAIL PROTECTED]
 wrote:
> Hello, newbie here.
>
> =A0 I'd like to know if there is a way to return a structure from a
> function, without creating any other intermediate variables. Also,
> this should be done such that the function can still be declared as
> inline.

Yes. Any ordinary function will likely do so. The compiler is allowed
to optimise this special case that has been given the name (N)RVO for
(Named) Return Value Optimisation and all recent compilers should be
able to use this strategy.
>
> =A0 More precisely, given the structure:
>
> struct TwoFields {
> =A0 =A0int Field_one;
> =A0 =A0int Field_two;
>
> }
>
> I want a function like the following one, but without the
> extra declaration, "TwoFields myReturnStruct;":
>
> inline TwoFields ReturnMyFields(int myInt) {
>
> TwoFields myReturnStruct;
>
> myReturnStruct.Field_one =3D myInt/2;
> myReturnStruct.Field_two =3D myInt%2;
>
> return myReturnStruct;
>
> }
>
This looks fine to me.

> =A0 The only reason I want this is speed efficiency, so if this is not
> worth doing please let me know.
I can sympathise with your attitude - I used to think that way
myself. But seriously: do you believe that the difference in execution
time for that function is going to make any difference?

/Peter
 




 16 Posts in Topic:
Returning a Struct from a Function
alex.j.k2@[EMAIL PROTECTE  2008-05-11 10:57:38 
Re: Returning a Struct from a Function
john <john@[EMAIL PROT  2008-05-11 20:02:06 
Re: Returning a Struct from a Function
alex.j.k2@[EMAIL PROTECTE  2008-05-11 11:18:16 
Re: Returning a Struct from a Function
"Alf P. Steinbach&qu  2008-05-11 20:45:46 
Re: Returning a Struct from a Function
peter koch <peter.koch  2008-05-11 11:19:09 
Re: Returning a Struct from a Function
alex.j.k2@[EMAIL PROTECTE  2008-05-11 11:33:42 
Re: Returning a Struct from a Function
alex.j.k2@[EMAIL PROTECTE  2008-05-11 12:36:41 
Re: Returning a Struct from a Function
Gianni Mariani <gi4nos  2008-05-11 22:33:22 
Re: Returning a Struct from a Function
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-12 16:31:55 
Re: Returning a Struct from a Function
ram@[EMAIL PROTECTED] (S  2008-05-12 16:42:36 
Re: Returning a Struct from a Function
"Victor Bazarov"  2008-05-12 12:55:52 
Re: Returning a Struct from a Function
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-12 16:59:34 
Re: Returning a Struct from a Function
James Kanze <james.kan  2008-05-13 01:50:00 
Re: Returning a Struct from a Function
"Alf P. Steinbach&qu  2008-05-13 10:53:15 
Re: Returning a Struct from a Function
mojmir <svobodamo@[EMA  2008-05-14 01:37:03 
Re: Returning a Struct from a Function
Richard Herring <junk@  2008-05-20 09:55:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:50:22 CDT 2008.