| JAI SINGH (66) | |
|
struct integer { int x; void set(int i) { this->x=i; return; } int get() { return this->x; } integer&operator+(const integer&v1,const integer &v2) { set(v1.get()+v2.get()); return (*this); } }; int main(void) { console::clear(); integer v1,v2,v3; output::string("enter a number"); v1.set(input::integer()); v2.set(input::integer()); v3.operator+(v1,v2); output::integer(v3.get()); output::string("="); output::integer(v1.get()); output::string("+"); output::integer(v2.get()); console::pause(); return 0; } what is wrong in the given code and why? | |
|
|
|
| vlad from moscow (3662) | |||||
|
As a member function this binary operator + is incorrect integer&operator+(const integer&v1,const integer &v2) { set(v1.get()+v2.get()); return (*this); } In fact it has three parameters. Implicit pointer to the object itself and two explicitly declared parameters. The operator as a member function shall be declared the following way
And moreover function get shall have the const qualifier
| |||||
|
Last edited on
|
|||||
| Disch (8615) | |||||||
|
No that is not correct. Binary operators take 2 parameters, one for each side of the operator. If you define your operator as part of a class, then the first parameters is automatically considered to be this, which means your function would only take one parameter.An example of this:
If the operator is defined globally, the 'this' pointer does not exist. Therefore both left and right sides must be passed to the function:
Note that in each case, the object being assigned to the sum is not represented. That is... c = a + b;.... only a and b are represented. c is not part of the + operator at all.Also note that I don't return a reference in my examples, I return a copy (value). This is because you do not want to modify 'this'.. but rather you want to create an entirely new object and return that. Finally... this is nonsense: v3.operator+(v1,v2);The proper way to do it would be like this:
v1 would be either 'this' or 'lhs' depending on whether the operator is global or not. And v2 would be 'rhs'. The new object that is generated by the + operator is returned, then gets assigned to v3 by the assignment operator. | |||||||
|
|
|||||||
| vlad from moscow (3662) | |
I'd like to append my previous message that to show how to call the operator. You can call it either asv3 = v1.operator+( v2 );or v3 = v1 + v2;
| |
|
Last edited on
|
|
| Disch (8615) | |
| dammit vlad, you ninja ;) | |
|
|
|
| Gorlash (21) | |
|
Question from another C++ newbie: Could someone tell me what header(s) are missing on this file, to make it build? I added <iostream>, but input, output, console are all undefined... | |
|
|
|
| vlad from moscow (3662) | |
| This can say only the author of the code because he uses its own headers as I see. But all you need is the class definition. I think you will can to add input and output statements youself. | |
|
Last edited on
|
|
| Gorlash (21) | |
|
Ahhhh... I get it now... Since vlad and Disch were both treating the OP's code as working code, I thought it was! (as in, it is complete and compiles as presented). It didn't occur to me that the original example actually was not complete! Since all I really wanted to know about was the operator+ , I switched to cstdio for I/O, and now I see it working! Thanks! | |
|
|
|
| JAI SINGH (66) | |
|
@vald & disch thanx to all I got it... | |
|
|
|