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++ Leda > Re: Calling ope...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 8 Topic 188 of 212
Post > Topic >>

Re: Calling operator + multiple times in the same expression.

by Victor Bazarov <v.Abazarov@[EMAIL PROTECTED] > Feb 1, 2005 at 06:28 PM

BigMan wrote:
> I get this error message
> 
> "error C2440: 'argument' : cannot convert from 'Type' to 'Type &'"
> 
> compiling the following piece of code with VC++ 7.1 (w/o language
> extensions!):
> 
> 
>
///////////////////////////////////////////////////////////////////////////
> 
> class Type
> {
> public:
> 	Type ( ) { }
> 	Type( Type& ) { }
> };
> 
>
///////////////////////////////////////////////////////////////////////////////
> 
> inline Type operator +
> (
> 	Type const&
> ,	Type const&
> )
> {
> 	Type Result;
> 	return Result;
> }
> 
>
///////////////////////////////////////////////////////////////////////////
> 
> inline Type operator -
> (
> 	Type const&,
> 	Type const&
> )
> {
> 	Type Result;
> 	return Result;
> }
> 
>
///////////////////////////////////////////////////////////////////////////////
> 
> int main
> (
> 	int,
> 	char*
> )
> {
> 	Type a, b, c;
> 	a - b;			// 1
> 	a - b + c;		// 2
> 	
> 	return 0;
> }
> 
>
///////////////////////////////////////////////////////////////////////////////
> 
> Line 1 compiles fine, but line 2 does not. VC++ 7.1 tries to call the
> copy ctor in order to compile line 2 and fails. I cannot see the
> reason to do so.
> 
> Please tell me if this code sould compile accoring to the standard. If
> not, explain why.

No, it should not.  A tem****ary is created to return the value from both
operators.  To bind a const reference to a tem****ary a copy constructor
has to be available (although it doesn't have to be used).  In your case
the copy constructor has the form T(T&), which when used to copy some
tem****ary object would cause a reference to non-const to bind to the
tem****ary, which is not allowed.  The fix would be to declare your copy
constructor as

     Type(Type const&);

if possible, of course.

V

      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 8 Posts in Topic:
Calling operator + multiple times in the same expression.
BigMan@[EMAIL PROTECTED]   2005-02-01 15:09:17 
Re: Calling operator + multiple times in the same expression.
"Abhishek Pamecha&qu  2005-02-01 18:23:20 
Re: Calling operator + multiple times in the same expression.
Victor Bazarov <v.Abaz  2005-02-01 18:28:35 
Re: Calling operator + multiple times in the same expression.
"Thomas Mang" &  2005-02-01 18:27:29 
Re: Calling operator + multiple times in the same expression.
BigMan@[EMAIL PROTECTED]   2005-02-02 15:02:26 
Re: Calling operator + multiple times in the same expression.
"Thomas Tutone"  2005-02-03 05:41:22 
Re: Calling operator + multiple times in the same expression.
kanze@[EMAIL PROTECTED]   2005-02-03 10:19:34 
Re: Calling operator + multiple times in the same expression.
"Thomas Mang" &  2005-02-05 20:44:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:40:00 CDT 2008.