This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Passing a reference to a value returned by a function
- From: "Ajay Bansal" <Ajay_Bansal at infosys dot com>
- To: "Mihnea Balta" <dark_lkml at mymail dot ro>,<gcc-help at gcc dot gnu dot org>
- Date: Tue, 11 Mar 2003 21:35:03 +0530
- Subject: RE: Passing a reference to a value returned by a function
One way id to create a variable & store the return value of
function_returning_struct in this variable as
type_t a = function_returning_struct();
printf("%d\n", function_taking_reference(a));
return 1;
-----Original Message-----
From: Mihnea Balta [mailto:dark_lkml at mymail dot ro]
Sent: Tuesday, March 11, 2003 9:27 PM
To: gcc-help at gcc dot gnu dot org
Subject: Passing a reference to a value returned by a function
Hi.
I have this piece of code (excuse the long function names, it's example
code
:) ):
#include <stdio.h>
struct type_t{
int a, b, c;
};
type_t function_returning_struct(){
type_t a;
a.a = 1;
return a;
}
int function_taking_reference(type_t& a){
if(a.a == 1)
return 1;
else
return 0;
}
int main(){
printf("%d\n",
function_taking_reference(function_returning_struct()));
return 1;
}
I'm trying to port code written like this from visualc to gcc. I suppose
that
visualc compiles the above piece of code because it creates a temporary
type_t that holds the return of function_returning_struct(), and passes
a
reference to that to function_taking_reference(). However, gcc 3.2.2
won't
compile that kind of code.
Is there any way to make gcc work with such code? I'm asking because
it's in a
lot of places, and adding a temp var by hand to all the places where
this
stuff occurs would be a pain.
Thanks.