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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -