Re: Copy assignment for derived class when base class has private members



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

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1
Topic: DEVELOP > c-Plus-Plus
User: "John Harrison"
Date: 06 Aug 2003 03:50:43 AM
Object: Re: Copy assignment for derived class when base class has private members
"Jochen Zeischka" <jochen.zeischka@rug.ac.be> wrote in message
news:bgqcq4$mg$1@gaudi2.UGent.be...

Hello,

if a base class has private members and you want to define a copy

assignment

operator for one of its derived classes, is the only way to do that by

using

GetPrivates() and SetPrivates(), as demonstrated below? Or are there more
elegant solutions?

There are more elegant solutions.


class Base {
X x;
Y y;

protected:
void GetPrivates(X&, Y&) const;
void SetPrivates(const X&, const Y&);

public:
Base();
Base(const Base&);
Base& operator=(const Base&);
};

Base::Base() {
; // construction code
}

Base::Base(const Base& b) {
; // copy construction code
}

Base& Base::operator=(const Base& b) {
x = b.x;
y = b.y;
return *this;
}

void Base::GetPrivates(X& xx, Y& yy) const {
xx = x;
yy = y;
}

void Base::SetPrivates(const X& xx, const Y& yy) {
x = xx;
y = yy;
}

class Derived: public Base {
public:
Derived();
Derived(const Derived&);
Derived& operator=(const Derived&);
};

Derived::Derived(): Base() {
; // construction code for Derived
}

Derived::Derived(const Derived& d): Base(d) {
; // copy construction code for Derived
}

Derived& Derived::operator=(const Derived& d) {
X xx;
Y yy;
d.GetPrivates(xx,yy);
this->SetPrivates(xx,yy);
// further code for Derived members
return *this;
}

The way the copy assignment is done here is quite consuming, since all
private Base members are copied twice. Once in GetPrivates and once in
SetPrivates. Using pointers is not possible, because I would have to give

up

the 'const' declaration for GetPrivates, which would mean that I would

lose

it for Derived::operator=(const Derived& d) too.

I have the impression that I'm doing something simple in a complex way...

Right
Derived& Derived::operator=(const Derived& d) {
Base::operator=(d);
// further code for Derived members
return *this;
}


Thanks for any advice!

Jochen


john
.


  Page 1 of 1


Related Articles
Re: Copy assignment for derived class when base class has privatemembers
Problem with sizeof, when using it with Base class pointer
Value of a pointer to base type when assigned to a derived type
When is a base class protected member not visible in a derived class?
What to do when base class is not specified as virtual?
Re: When is virtual fn def in base cls required?
Automatic conversion to base class when calling constructor.
Format of compiler generated derived destructor when base has 'virtual ~base() throw():"
confusion involving pointer to pointer when converting from derived to base
Re: When is virtual fn def in base cls required?
accessing base class members when base is template
Problem when using template as the base class?
Cannot match void (T::* ptr)() when base class of T defines function
Why ambiguous base when one is inherited private?
Re: When is virtual fn def in base cls required?
 

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