Question about inheritance in c++

D

Dotanitis

Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;
};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.
2. how can i protect this code and make this program fail in the
compilation phase?

Thanks,
Dotan
 
J

John Harrison

Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;
};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.

It doesn't but you used a cast to fool the compiler.
2. how can i protect this code and make this program fail in the
compilation phase?

C++ isn't that sort of language, you don't get the protection you do in
some languages. The simple advice is don't use casts, or when you must
use them, use C++ casts instead of C casts, they're a little bit safer.

john
 
D

Dotanitis

It doesn't but you used a cast to fool the compiler.


C++ isn't that sort of language, you don't get the protection you do in
some languages. The simple advice is don't use casts, or when you must
use them, use C++ casts instead of C casts, they're a little bit safer.

john

Thanks,
1. why it does'nt crush on the runtime phase?
2. can you show me the differ from c casting style & c++?

dotan
 
J

John Harrison

Thanks,
1. why it does'nt crush on the runtime phase?

When you break the rules of C++ usually what you get is 'undefined
behaviour'. Undefined behaviour means anything could happen with your
program. It could run, it could crash, it could run but print out
garbage, anything can happen. If you do this kind of thing too often I
promise you that your programs will crash, but they are not *required*
to crash.
2. can you show me the differ from c casting style & c++?

C++ has four new styles of cast.

static_cast<CB*>(a1)->foo();
reinterpret_cast<CB*>(a1)->foo();
const_cast<CB*>(a1)->foo();
dynamic_cast<CB*>(a1)->foo();

They are all designed to do one kind of casting, and if you use them to
do a different kind of casting you will get a compile error.

static_cast is for related types (it would work in your example because
CB inherits from CA but it wouldn't work if CB and CA were unrelated)

reinterpret_cast is for unrelated types, it works pretty much anytime
and is closest to the C style cast.

But even reinterpret_cast cannot remove const, const_cast is for that.

Finally dynamic_cast is something new. It is for polymorphic types (i.e.
classes with virtual functions) and determines at runtime whether a cast
can be made.

john
 
M

mathstar

Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;

};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}

};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();

}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.
2. how can i protect this code and make this program fail in the
compilation phase?

Thanks,thi
Dotan

Hello Dotan
Your programe maybe can't pass compilation phase,because the
compiler isn't able to find the declaration of "uid".I modified this
programe and let "uid" be the data menber of class CB.Then my programe
can get the same result as yours.The reason for this result is that
the constructor of class CB wasn't called,so "uid" wasn't initialized
and the value of it is uncertaint.
Changing the pointer of bass class to the ones of derived class is
unsafe except that you could make sure the object to which this
pointer points is derived class object.You can use operator
"dynamic_cast" to finish this work,but "dynamic_cast" requires the
bass class to have visual functions,because the compiler gets RTTI
from the visual tables.
Thanks
 
B

Bo Persson

Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;
};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.

You used a C-style cast here:

((CB*) a1)->foo();

That is a very powerful tool, telling the compiler to just "shut up and do
it!".

You are not asking the compiler to convert a1 into a CB*, you tell the
compiler that it already IS a CB*.
2. how can i protect this code and make this program fail in the
compilation phase?

You shouldn't tell lies to the compiler. :)

If you had tried the C++ way, and done a static_cast<CB*>(a1), the compiler
would have told you that it cannot do that. You told it to do it anyway!


Bo Persson
 
G

Grizlyk

Bo said:
You used a C-style cast here:

((CB*) a1)->foo();

That is a very powerful tool, telling the compiler to just "shut up and do
it!".

You are not asking the compiler to convert a1 into a CB*, you tell the
compiler that it already IS a CB*.

Note, it is _always conversion_ that, you are asking the compiler to do with
"(CB)a1", but what concrete kind of conversion (reinterpret, static etc )
will be done is unknown (implicit).

_Conversion_ is not the same as "a1" _treated already IS_ a CB*, in general
case of non-POD-pointers "(CB)a1" can be not the same as "*((CB*)(&a1))".


--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top