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

Discussion in 'C++' started by John Harrison, Aug 6, 2003.

  1. "Jochen Zeischka" <> wrote in message
    news:bgqcq4$mg$...
    > 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::eek:perator=(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::eek:perator=(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::eek:perator=(const Derived& d) too.
    >
    > I have the impression that I'm doing something simple in a complex way...


    Right

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

    >
    > Thanks for any advice!
    >
    > Jochen
    >
    >


    john
     
    John Harrison, Aug 6, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Heinz Buchegger
    Replies:
    3
    Views:
    540
    Karl Heinz Buchegger
    Aug 6, 2003
  2. CoolPint
    Replies:
    8
    Views:
    1,017
    Jeff Schwab
    Dec 14, 2003
  3. Replies:
    1
    Views:
    412
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    400
    Victor Bazarov
    May 23, 2007
  5. Fraser Ross
    Replies:
    7
    Views:
    565
    James Kanze
    Aug 7, 2007
Loading...

Share This Page