please use code tags ^^
1 2 3 4 5
|
CVector CVector::operator+ (CVector param) { // OK
CVector temp; // OK
temp.x = x + param.x; // OK Understood in the context of c = a + b is a.operator + (b);
temp.y = y + param.y; // OK Understood in the context of c = a + b is a.operator + (b);
return (temp); // Won't this local object get destroyed now?
|
Yes, the local 'temp' is destroyed, but because the return value is a CVector (ie: by value, not by reference), a copy of it is made.
If the return value were CVector* or CVector&, then you are correct in thinking this would be a problem, because 'temp' would be destroyed once the function exists, and the returned pointer/reference would be bad
|
int CDummy::isitme (CDummy& param) // isitme takes a reference to a CDummy as a paramter ?
|
Yes. A reference is similar to a pointer, but with different syntax. This allows the called function to manipulate the passed object, rather than it getting a copy of the object. It might be best to think of a reference as BEING the referred object, not simply pointing to it.
Simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void func(int& foo)
{
foo = 5;
}
int main()
{
int v = 0;
func(v);
cout << v; // outputs '5' because 'func' changed 'v'
return 0;
}
|
[codee]if (¶m == this) return true; // if the address of the reference == the address of the object ?? Eh?[/code]
'param'
is the passed object. Therefore '¶m' is the address of the passed object (not the address of the reference).
else return false; // Return false on an int?
You're right. That's stupid. The function should return a bool.
bools can be implicitly cast to integers, though. 'false' == 0 and 'true' == 1. But ew. That example sucks.
EDIT -- blah I'm too slow!