![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Unknown
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1
![]() |
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: c++ Syntax (Toggle Plain Text)
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
__________________
------------------------------------------------------------------------- I thought what I'd Do was, I'd pretend to be one of those deaf mutes ------------------------------------------------------------------------ |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2008
Location: India
Posts: 3
Rep Power: 0
![]() |
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
c++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#4 | |
|
Unknown
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1
![]() |
Re: copy constructors explanation and o.overloading
Quote:
__________________
------------------------------------------------------------------------- I thought what I'd Do was, I'd pretend to be one of those deaf mutes ------------------------------------------------------------------------ |
|
|
|
|
|
|
#5 |
|
Unknown
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1
![]() |
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?
__________________
------------------------------------------------------------------------- I thought what I'd Do was, I'd pretend to be one of those deaf mutes ------------------------------------------------------------------------ |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#7 |
|
Unknown
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1
![]() |
Re: copy constructors explanation and o.overloading
thanks again grumpy.... now i get it......
__________________
------------------------------------------------------------------------- I thought what I'd Do was, I'd pretend to be one of those deaf mutes ------------------------------------------------------------------------ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|