Re: Copy assignment for derived class when base class has privatemembers



 DEVELOP > c-Plus-Plus > Re: Copy assignment for derived class when base class has privatemembers

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.




  Page 1 of 1

1

 


Related Articles
Re: Copy assignment for derived class when base class has private members
Cannot match void (T::* ptr)() when base class of T defines function
Re: When is virtual fn def in base cls required?
Format of compiler generated derived destructor when base has 'virtual ~base() throw():"
Re: When is virtual fn def in base cls required?
Problem when using template as the base class?
Why ambiguous base when one is inherited private?
accessing base class members when base is template
Problem with sizeof, when using it with Base class pointer
How to do MFC code change when a field type in the underlying data base is changed
What to do when base class is not specified as virtual?
Value of a pointer to base type when assigned to a derived type
confusion involving pointer to pointer when converting from derived to base
Why polymorph fails when virtual function is decleared private in base class and public in derived class?
When is a base class protected member not visible in a derived class?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER