"Paul" <> wrote in message
news:zMtmp.12497$2...
>
> "crea" <> wrote in message
> news
mrmp.16671$2...
>>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??
>
> Basically you're dereferencing an uninitialised pointer.
>
> B* b;
> b->var=77;
>
> It just happens that the pointer is a member of a class, and you've
> type-casted it. The pointer could point to anything. °_°
Sorry I overlooked the a1.a = &a2; assginement , my bad.