0

Consider:

class Base{
    private:
        int general_attribute;
};

class Derived : public Base{
    private:
        int specific attribute;

    public:
        void reassign(Base value);
};

Is there a general, maintainable way in which I can write reassign such that only the base class members (i.e. general_attribute) will be copied from the base-class instance?

If I understand correctly, writing

void Derived::reassign(Base value){
     *this = value;
}

won't do because it will result in the loss of specific_attribute because of object slicing.

What are some recommended solutions?

user1892304
  • 617
  • 1
  • 6
  • 11

0 Answers0