![]() |
copy constructors explanation and o.overloading
I just need some little explanation about this snippet, i did some little experiment in the class "value" and when i compiled the source file, error pops out, even though in my own logic, there is nothing wrong with it, i came with this book and im trying to learn copy constructors and operator overloading and i did some experimenting on the code:
This is the original code: :
now what I did is that on line 50 i replaced it to inline Value operator+ (Value const& v1,Value const& v2)then the error is "must take zero to one argument" and i also replaced line 50 to Value& operator+ (Value const& v1,Value const& v2)and still same error.... im wondering what's the explanation behind this, i just copied line 34... Thanks |
Re: copy constructors explanation and o.overloading
I am not sure but you can try rewriting your code from line 50 as keep in mind to remove the friend keyword
:
|
Re: copy constructors explanation and o.overloading
operator+() can be a member function of the class that accepts one argument, so "x = a + b" is effectively "x = a.operator+(b)" or a non-member, so "x = a + b" is effectively "x = operator+(a, b);".
The member function version can only accept one argument. The non-member version can only accept two. The friend declaration within the class declares the non-member version as a friend of the class, which accepts two arguments. Removing the friend keyword makes the declaration into a member function, which is only allowed to accept one argument. However, your declaration has two .... hence the error. The inline keyword is unrelated to your error. |
Re: copy constructors explanation and o.overloading
Quote:
|
Re: copy constructors explanation and o.overloading
anway that means that operator+() can only be used to accept one argument? so everytime you overload an operator you'll always think that it must accept one argument?
|
Re: copy constructors explanation and o.overloading
Addition is a binary operation: it acts on two objects and returns a result.
The only difference between implementing an operator+() as a member function or as a non-member function is how the two objects are passed to the function. Unrelated to the topic: operator+(), like operator-() can also be a unary operation. |
Re: copy constructors explanation and o.overloading
thanks again grumpy.... now i get it......
|
| All times are GMT -5. The time now is 9:23 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC