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); ??
ref arg initialized from constructor
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?
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?
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.
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.
Quote:Original post by Endurion
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?
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.
That answers that - thanks!
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.
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.
Quote:Original post by Shannon Barber
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement