I know that with multiple inheritance, the value of a pointer can be changed. But is this the case with one inheritance? Or with POD types for that matter?
You probably know a classic example:
#include <iostream>
using std::cout;
struct Base1 { virtual void f1() {} };
struct Base2 { virtual void f2() {} };
struct Derived : public Base1, public Base2 { virtual void g() {} };
int main() {
Derived d{};
auto *pd = &d;
auto pb1 = (Base1*)pd;
auto pb2 = (Base2*)pd;
cout << pd << "\n";
cout << pb1 << "\n";
cout << pb2 << "\n";
}
So far, so good, and this is a good old compiler practice. Objects are laid out in such a way that the "root" Base2
has an offset to Derived
, which can be printed when viewing the actual value of the pointer. This is not a problem if refrain from performing void*
and reinterpret_cast
s.
And as far as I know in practice, this only happens with multiple inheritance.
: " "? POD?