Why does the default copy constructor not call the base constructor of monster
but when I include a user-defined copy-constructor in troll
it calls the parent (i.e.: monster
) constructor?
As I think it works as follow: Create the base object and after that copy the elements inside.
Here is a sample code:
using std::cout;
struct monster {
monster() {
cout << "a monster is bread\n";
}
~monster() {
cout << "monster killed\n";
}
void health() {
cout << "immortal?\n";
}
virtual void attack() {
cout << "roar\n";
}
};
struct troll: monster {
troll() {
cout << "a troll grows\n";
}
~troll() {
cout << "troll petrified\n";
}
protected:
int myhealth { 10 };
};
struct forum_troll: troll {
forum_troll() :
troll { } {
cout << "not quite a monster\n";
}
~forum_troll() {
cout << "troll banned\n";
}
};
int main() {
cout << "a ------\n";
forum_troll ft { };
cout << "copy \n \n";
troll t { ft };
cout << "end \n \n";
}