# ref arg initialized from constructor

This topic is 4445 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

class A
{
public:
A(int x);
//...
};

void myfunc(A & a) {
//...
}

int main()
{
myfunc(A(2));
return 0;
} 
myfunc operates on a reference... but no variable inside int main actually holds the object that was initialized with A(2). So why isn't this a compiler error? Is it equivalent to A a = A(2); myfunc(a); ??

##### Share on other sites
VS 2003 will complain at this if you set the warning level to 4. I do use this construct often though. As i see it the object stays alive until the function call returns.

But since this will be a warning at level 4 i'd like to ask the gurus out there is this a valid piece of code?

##### Share on other sites
I'm using g++ 4.0.1 with -Wall and it doesn't give a peep.

##### Share on other sites
A(2) creates a temporary instance of class A initialized with 2.

It's valid but not best-practice.

myfunc(const A& a) would say it's ok to take a temporary since we're not going to change it anyway.

As-is though, member funtcions of A could have side-effects on other objects or so it could affect the state of the system.

##### Share on other sites
Quote:
 Original post by EndurionVS 2003 will complain at this if you set the warning level to 4. I do use this construct often though. As i see it the object stays alive until the function call returns.But since this will be a warning at level 4 i'd like to ask the gurus out there is this a valid piece of code?

This is valid AFAIK. A mutable, temporary variable is created, modified, and destroyed. The warning is there because more often than not, if you need a non-const reference, you were expecting to perform modifications on a variable and have them reflected in the calling code (otherwise it might as well have been a local variable). If discarding this is truely your intention, you can make this clear:

A temp(2);myfunc( temp );//note: temp discarded

If it dosn't make sense for your function to modify the temporary, then myfunc should accept a reference-to-const :

void myfunc(const A & a)[

Either of these should eliminate the warning.

##### Share on other sites

BTW: I have been using reference to const ... sorry I forgot to put that in my original code listing. For g++, it gives an error if it is not const.

##### Share on other sites
Quote:
 Original post by Shannon BarberA(2) creates a temporary instance of class A initialized with 2.It's valid but not best-practice.myfunc(const A& a) would say it's ok to take a temporary since we're not going to change it anyway.As-is though, member funtcions of A could have side-effects on other objects or so it could affect the state of the system.

It's not valid. It's a non-standard Microsoft extension.

Passing a non-const reference to a temporary object is illegal.

In VS2005 with warning level 4:
warning C4239: nonstandard extension used : 'argument' : conversion from 'A' to 'A &'

With non-standard extensions turned off (/Za):
error C2664: 'myfunc' : cannot convert parameter 1 from 'A' to 'A &'

edit: Also note that the MS compilers only allow this for user defined types.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 27
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631407
• Total Posts
2999914
×