• Advertisement
Sign in to follow this  

ref arg initialized from constructor

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement