DEVELOP > c-Plus-Plus > Re: Copy assignment for derived class when base class has privatemembers
| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Karl Heinz Buchegger" |
| Date: |
06 Aug 2003 03:44:54 AM |
| Object: |
Re: Copy assignment for derived class when base class has privatemembers |
Jochen Zeischka wrote:
I have the impression that I'm doing something simple in a complex way...
Right.
Rule #1: Make sure you put code in the class where it belongs!
It is *not* the business of the Derived class to take care of
the exact details of the Base class: what needs to be assigned
and how to do it. It is the business of the Base class to take
care of that. All you need to do in the Derived operator=, is to
call the Base::operator= to give it a chance to do it's work.
Derived& Derived::operator=(const Derived& d)
{
Base::operator=( d ); // let the base class operator=
// do it's work
// and now handle the Derived member variables.
return *this;
}
Note: Since in this specific example, Derived has no work to
do on it's own, you wouldn't need the whole Derived::operator=
at all. The compiler generated one does exactly the same thing.
But I understand that this is just a simplified example for
posting purposes.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
| User: "Jochen Zeischka" |
|
| Title: Re: Copy assignment for derived class when base class has private members |
06 Aug 2003 04:01:33 AM |
|
|
Thanks a lot!
I was quite sure there had to be a simpler way, but I never would have
thought about using the copy assignment as "Base::operator=(d);"
I was thinking of something like "*this = Base(d);" to call Base::operator=,
but that is ofcourse non-sense, as the return type doesn't match *this.
Thanks again!
Jochen
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3F30C006.1EA601AB@gascad.at...
Jochen Zeischka wrote:
I have the impression that I'm doing something simple in a complex
way...
Right.
Rule #1: Make sure you put code in the class where it belongs!
It is *not* the business of the Derived class to take care of
the exact details of the Base class: what needs to be assigned
and how to do it. It is the business of the Base class to take
care of that. All you need to do in the Derived operator=, is to
call the Base::operator= to give it a chance to do it's work.
Derived& Derived::operator=(const Derived& d)
{
Base::operator=( d ); // let the base class operator=
// do it's work
// and now handle the Derived member variables.
return *this;
}
Note: Since in this specific example, Derived has no work to
do on it's own, you wouldn't need the whole Derived::operator=
at all. The compiler generated one does exactly the same thing.
But I understand that this is just a simplified example for
posting purposes.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: Copy assignment for derived class when base class has privatemembers |
06 Aug 2003 04:18:35 AM |
|
|
Jochen Zeischka wrote:
Thanks a lot!
I was quite sure there had to be a simpler way, but I never would have
thought about using the copy assignment as "Base::operator=(d);"
An operator is just another function with a fancy name :-)
I was thinking of something like "*this = Base(d);" to call Base::operator=,
but that is ofcourse non-sense, as the return type doesn't match *this.
That's not the reason. The problem is that *this is still a Derived object
and thus the operator= of Derived would be called -> endless recursion.
But casting 'this' to a Base* would have worked.
*((Base*)this) = d;
Nevertheless: calling the operator in the shown way is IMHO simpler and
it avoids a cast, which is always a good thing.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: Copy assignment for derived class when base class has privatemembers |
06 Aug 2003 06:44:44 AM |
|
|
Jochen Zeischka wrote:
The "Base::operator=(d);" solution works perfectly fine and is absolutely
what I'm looking for. Just for fun though I tried the type-cast version
"*((Base*)this) = d;" and it compiles but it gives incorrect results.
Just to inform you ;-)
Hmm. This works for me:
#include <iostream>
using namespace std;
class Base
{
public:
Base( int i ) : m_Test( i ) {}
Base& operator=( const Base& Arg ) { m_Test = Arg.m_Test; return *this; }
int m_Test;
};
class Derived : public Base
{
public:
Derived( int i = 0 ) : Base( i ) {}
Derived& operator=( const Derived& Arg ) { *(Base*)this = Arg; return *this; }
};
int main()
{
Derived a, b( 4 );
cout << a.m_Test << endl; // expected 0
a = b;
cout << a.m_Test << endl; // expected 4
return 0;
}
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
|
|
|

|
Related Articles |
|
|