c++ - Assigning class to base class, how do do it? -
i'm confused assigning base object of derived class. have type:
class base { // stuff omitted brevity }
and derived class
derived : public base { // stuff omitted }
and have situation arises this:
derived = base;
is possible? operation called? how such thing?
thanks help.
this ordinary user-defined assignment looks enough "slicing", treating base class if member, confuse unwary. it's not slicing. situation have, 2 classes might not related, , define assignment way.
struct d : b { d &operator=(const b &b_) { /*do work here*/; return *this; } };
you might able use base class's copy-assignment operator, or might not:
struct dx : b { dx &operator=(const b &b_) { this->b::operator=(b_); // more work return *this; } };
but compiler gives no special treatment, it's same function call.
Comments
Post a Comment