Given the following code:
namespace Example1 {
class A {
public:
A() {}
virtual ~A() {}
private:
float data_A;
};
class B {
public:
B() {}
virtual ~B() {}
protected:
float data_B;
};
class Derived : public A, public B {
public:
Derived() {}
virtual ~Derived() {}
protected:
float data_Derived;
};
}
int main (void)
{
using namespace Example1;
B* pb = new Derived;
delete pb;
}
pb
should now point to the B
part of the Derived
object.
But the derived object also derives from A
, means it has A
sub-object.. and that A
sub-object should be "first" because the Derived
class first inherits from A
.
How does the compiler approves that? what does it add in order to make it work correctly?
and also, how does it free the memory correctly when deleting the object?