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 8 of 16 Topic 45864 of 47989
Post > Topic >>

Re: Returning a Struct from a Function

by Gianni Mariani <gi4nospam@[EMAIL PROTECTED] > May 11, 2008 at 10:33 PM

alex.j.k2@[EMAIL PROTECTED]
 wrote:
> Hello, newbie here.
> 
>   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.
> 
>   More precisely, given the structure:
> 
> struct TwoFields {
>    int Field_one;
>    int 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 = myInt/2;
> myReturnStruct.Field_two = myInt%2;
> 
> return myReturnStruct;
> }
> 
>   The only reason I want this is speed efficiency, so if this is not
> worth doing please let me know.

Premture optimization alert !

BTW - if you really want to do this - imho this is the easy way - and 
BTW - RVO will prolly happen.

TwoFields & f = ReturnMyFields(v);

Notice there is a tem****ary returned which is getting bound to a 
reference.  In theory, this will only get constructed once - in the 
function ReturnMyFields;  If your compiler is really smart, (and olot of 
them are) it will never hit memory and exist only in registers.

Unless you "know" this is an issue for you, don't bother worrying about
it.
 




 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 Sat Oct 11 15:00:33 CDT 2008.