C
crea
I know its simple, but I cannot find the answer from books because its a
special case.
Here:
///////////////////////
class A
{
public:
A* a;
};
class B : public A
{
public:
int var;
};
A a1, a2;
a1.a = &a2;
((B*)a1.a)->var = 77;
////////////////
I compiled and it works.
How/why can a1.a set B classes member variable's value even though there is
not even a one object created from B? How is it possible.
So all objects are from class A and there is no object created from B and we
are still using B's members. I thought that this var-variable is not even
allocated from the memory because there is no B object created??
special case.
Here:
///////////////////////
class A
{
public:
A* a;
};
class B : public A
{
public:
int var;
};
A a1, a2;
a1.a = &a2;
((B*)a1.a)->var = 77;
////////////////
I compiled and it works.
How/why can a1.a set B classes member variable's value even though there is
not even a one object created from B? How is it possible.
So all objects are from class A and there is no object created from B and we
are still using B's members. I thought that this var-variable is not even
allocated from the memory because there is no B object created??