Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple class inheritance question

Reply
Thread Tools

Simple class inheritance question

 
 
Paul
Guest
Posts: n/a
 
      04-05-2011

"Paul" <> wrote in message
news:zMtmp.12497$2...
>
> "crea" <> wrote in message
> newsmrmp.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.

 
Reply With Quote
 
 
 
 
MikeWhy
Guest
Posts: n/a
 
      04-05-2011

"crea" <> wrote in message
news:QJsmp.2193$2...
>
> "Leigh Johnston" <> wrote in message
> news: ...
>>
>> Just because it compiles and seems to work does not mean that it is
>> correct; it is incorrect (a bug) as it invokes UB.
>>
>>>
>>> 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.

>>
>> It is not possible; what you have written is buggy code.
>>

>
> Seriously, I am telling the truth: I have Visual C++ 2008, and it compiles
> and lets me to use it as normal parameter after that. If I call
> ((B*)a1.a)->var later on, it will give 77!


Seriously, we're not the ones with the miscomprehension. Your code works,
insofar as it explicitly discards type checking with a C-style typecast. The
question is what happens if you change B as follows:

class B : public A
{
public:
char foo[1024];
int var;
};

If it still doesn't fail, make foo even larger. You should conclude from
this that only a fool subverts type checking. If you rewrite your code to
honor the type, ...

B* b = a1.a; // fails. A is not a B.
b->var = 77;

.... the compiler should complain appropriately. Ignore or subvert at your
own peril.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Simple example of error handling using base class inheritance? Doug ASP .Net 1 07-03-2006 09:07 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57