Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > operator= for derived class

Reply
Thread Tools

operator= for derived class

 
 
MojoRison
Guest
Posts: n/a
 
      01-21-2005
struct A{
A& operator=( const A& a ){ i = a.i; return *this; }

private:
int i;
};

struct B : public A{
B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?

private:
int j;
};

In the above code, what would be the standard way of coping A::i in
B:perator= ?


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-21-2005
MojoRison wrote:
> struct A{
> A& operator=( const A& a ){ i = a.i; return *this; }
>
> private:
> int i;
> };
>
> struct B : public A{
> B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?
>
> private:
> int j;
> };
>
> In the above code, what would be the standard way of coping A::i in
> B:perator= ?



For the classes in their presented form, there is no need for operator=.
But you probably already knew that.

The simple way is to call the A:perator= directly inside B:perator= :

B& operator=(const B& b) { A:perator=(b); j = b.j; return *this }

Victor
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-21-2005
MojoRison wrote:

> struct A{
> A& operator=( const A& a ){ i = a.i; return *this; }
>
> private:
> int i;
> };
>
> struct B : public A{
> B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?
>
> private:
> int j;
> };
>
> In the above code, what would be the standard way of coping A::i in
> B:perator= ?


Firstly, for such classes you don't need to implement the copy
assignment operator explicitly. The one automatically provided by the
compiler will do exactly what you want.

If you still want to implement these operators manually, then the proper
way to do it would be calling the inherited copy assignment operator
('A:perator=') from B's implementation

B& operator=( const B& b ) {
A:perator=(b);
j = b.j;
return *this;
}

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-21-2005
MojoRison wrote:
> struct A{
> A& operator=( const A& a ){ i = a.i; return *this; }
>
> private:
> int i;
> };
>
> struct B : public A{
> B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?
>
> private:
> int j;
> };
>
> In the above code, what would be the standard way of coping A::i in
> B:perator= ?
>
>


I would make the operator= in the parent class
protected. This would prevent "slicing", where only the
parent portion is copied. See [1] below.

One method of implementing assignment operator in child
classes is:
Child&
Child ::
operator=(const Child& ch)
{
if (this != &child) /* guard against self assignment */
{
(Parent&)(*this) = (Parent&)(ch);
/* assignment of child members */
}
return *this;
}
The above fragment comes from Effective C++ or More Effective C++
by Scott Meyers.

[1] When parents allow public assignment, one can assign the
parent portion of child A to child B, using pointers to
the parent class.
struct Parent
{
Parent& operator=(const Parent& p);
};

struct Son : Parent
{
Son& operator=(const Son& s);
};

struct Daughter : Parent
{
Daughter& operator=(const Daughter& d);
};

/* ... */

Son bill;
Son jack;
Daughter amy;
Parent * p_male = &bill;
Parent * p_female = &amy;
Parent * p_brother = & jack;
*p_brother = *p_male; /* Are the child members copied? */
/* One _might_ think so... */
*p_male = *p_female; /* copies only parent portion */
/* Do you want this allowed? */

This has cost me many hours of debugging, so I've learned
to make assignment operators of base classes as protected.
to prevent the above scenario from occurring.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
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 Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments