Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Copy assignment for derived class when base class has privatemembers

Reply
Thread Tools

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

 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-06-2003


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:perator= to give it a chance to do it's work.

Derived& Derived:perator=(const Derived& d)
{
Base:perator=( 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:perator=
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jochen Zeischka
Guest
Posts: n/a
 
      08-06-2003
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:perator=(d);"

I was thinking of something like "*this = Base(d);" to call Base:perator=,
but that is ofcourse non-sense, as the return type doesn't match *this.

Thanks again!

Jochen



"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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:perator= to give it a chance to do it's work.
>
> Derived& Derived:perator=(const Derived& d)
> {
> Base:perator=( 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:perator=
> 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
> (E-Mail Removed)



 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-06-2003


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:perator=(d);"


An operator is just another function with a fancy name

>
> I was thinking of something like "*this = Base(d);" to call Base:perator=,
> 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
(E-Mail Removed)
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-06-2003


Jochen Zeischka wrote:
>
> The "Base:perator=(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
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
derived class and base class, member assignment, which goes first user C++ 1 07-11-2005 04:25 AM
Format of compiler generated derived destructor when base has 'virtual ~base() throw():" qazmlp C++ 1 04-10-2005 03:09 PM
Re: Copy assignment for derived class when base class has private members John Harrison C++ 0 08-06-2003 08:50 AM



Advertisments