| in this example in the function operator + the CVector temp is not initialized! |
That's a flaw in the default constructor for CVector, which should initialise its data members to suitable defaults.
In the operator +, it's not a problem, because the values the data members of temphave values assigned to them immediately after construction, in lines 15 - 16.
It's very straightforward. The two vectors being added are
this and
param. The result of the addition is stored in
temp, and the operator then returns the results. So if we have:
1 2 3 4 5
|
CVector a(1, 2);
CVector b(3, 4);
CVector c;
c = a + b;
|
then line 5 is equivalent to:
c = a.operator+(b);
So
a is
this,
b is
param, and the contents of
temp get copied into
c.
| why you need to write this fucntion as a member of class? |
You don't. You can implement it as a free function too, if you prefer.
| the left side gets copied to temp? |
See above.
| what will happen if you overload +operator? now the right side will get copied to the CVector temp inside the funnction?? |
Overloading the operator would have the same effect as overloading any function - it allows you to define another operator that takes different arguments, or have different const-ness. It won't change the fact that the left-hand operand becomes
this, and the right-hand operand becomes
param.
why you declare the operator + func outside the class? can you overload operator without defining his classes (for his arguments)
HELP!
can you also give me another example of operator overloading?
What is *(this) when should i use it and return it from function?? |
Are these homework questions? We're not a homework service.