G
grahamo
Hi,
I have something that I'd like to clarify here.... there are two
issues. I have simple dummy class called base which (everything is
inlined just for readability) defines these methods...
friend base operator+ (const base& lhs, const base& rhs)
{
base tmp;
tmp.l = lhs.l + rhs.l;
cout << "friend base operator+ (const base& lhs, const base&
rhs)" << endl;
return tmp;
}
base& operator+(const base& rhs)
{
cout << " base& operator+(const base& rhs) " << endl;
this->l+=rhs.l;
return *this;
}
I have put cout statements in my default ctor and copy ctor to see
when they are called.
Now in my main() function, I have a routine thus;
base f(const base& b1, const base& b2)
{
cout << "base f(const base& b1, const base& b2)" << endl;
base tmp = b1 +b2;
return tmp;
};
My question is this. Within the function f above, I would expect to
see output indicating;
1) that the friend base operator+ (const base& lhs, const base& rhs)
function will be called first.
2) then the copy ctor will happen for base tmp = b1 +b2;
3) then a copy ctor will be called for the return tmp statement.
4) default dtor called for the temporary created upon f exit.
thats not what I'm seeing however, my gnu c++ compiler is showing;
1) default ctor called for base tmp = b1 +b2;
2) followed by friend base operator+ (const base& lhs, const base&
rhs) function
I don't see the ctor being called for the return value followed by the
dtor for the local tmp variable. Here's my thinking....
1) when doing declaration and assignment on the same line, the copy
ctor is called only if the rhs of "=" contains a single value. If
there's any operations to be done, then the default ctor is used and
then thats assigned into. i.e. base tmp = b1 +b2 doesn't call copy
ctor but base tmp = b2; would
2) The compiler is doing some funky optimisation wrt the copy of tmp
being made for the return value. This point is the one that concerns
me most. (yes, all my cout statements use endl at the end to flush the
buffer.
thanks a million
GrahamO
I have something that I'd like to clarify here.... there are two
issues. I have simple dummy class called base which (everything is
inlined just for readability) defines these methods...
friend base operator+ (const base& lhs, const base& rhs)
{
base tmp;
tmp.l = lhs.l + rhs.l;
cout << "friend base operator+ (const base& lhs, const base&
rhs)" << endl;
return tmp;
}
base& operator+(const base& rhs)
{
cout << " base& operator+(const base& rhs) " << endl;
this->l+=rhs.l;
return *this;
}
I have put cout statements in my default ctor and copy ctor to see
when they are called.
Now in my main() function, I have a routine thus;
base f(const base& b1, const base& b2)
{
cout << "base f(const base& b1, const base& b2)" << endl;
base tmp = b1 +b2;
return tmp;
};
My question is this. Within the function f above, I would expect to
see output indicating;
1) that the friend base operator+ (const base& lhs, const base& rhs)
function will be called first.
2) then the copy ctor will happen for base tmp = b1 +b2;
3) then a copy ctor will be called for the return tmp statement.
4) default dtor called for the temporary created upon f exit.
thats not what I'm seeing however, my gnu c++ compiler is showing;
1) default ctor called for base tmp = b1 +b2;
2) followed by friend base operator+ (const base& lhs, const base&
rhs) function
I don't see the ctor being called for the return value followed by the
dtor for the local tmp variable. Here's my thinking....
1) when doing declaration and assignment on the same line, the copy
ctor is called only if the rhs of "=" contains a single value. If
there's any operations to be done, then the default ctor is used and
then thats assigned into. i.e. base tmp = b1 +b2 doesn't call copy
ctor but base tmp = b2; would
2) The compiler is doing some funky optimisation wrt the copy of tmp
being made for the return value. This point is the one that concerns
me most. (yes, all my cout statements use endl at the end to flush the
buffer.
thanks a million
GrahamO