I just encountered a snippet of code which seems quite strange to me(see below as a minimal example), the derived::base
is a reference to another object of type of base
, can someone help me to answer the questions in the comments?
class base{
public:
int a;
int b;
};
class derived : public base{
public:
double c;
void run(const base & bs){
((base &) *this) = bs; // what does this line do?
// Is derived::base now a copy of bs?
// If yes, but why not write ((base) *this) = bs?
// if not, then derived::base is a reference to bs,
// then does it mean the memory of derived::base
// and members of derived are no longer contiguous?
std::cout << "a = " << a << std::endl;
}
};
PS
comments by @LightnessRacesinOrbit helped a lot to clear the questions, but I can only accept an answer post, the best is by @WhiZTiM