Code:
#include
class myc {
int dummy;
public:
int si(){return sizeof(*this);}
};
class d_myc : public myc {
int d_dummy;
};
int mai
sizeof
is resolved at compile-time, not run-time. So sizeof(*this)
is equivalent to sizeof(myc)
.
I do not know "Why were your expectations wrong" because I cannot read minds :). But if you wrote sizeof(myc), sizeof(d_myc) the compiler would generate exactly the same code as for what you have coded above. myc has 1 int, assuming 32bit, so 4 bytes. d_myc has 2 ints => 8 bytes.
This is resolved at compile time:
class myc {
int dummy;
public:
int si(){return sizeof(*this);}
};
i.e. *this
is always myc and will never be d_myc
.
To get what you want you will have to override the function in d_myc
to do the same in the derived as the base. This is because sizeof(d_myc)
includes the base class too.