By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Question about inheritance in c++

P: n/a
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

Mar 11 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Do*******@gmail.com wrote:
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
Mar 11 '07 #2

P: n/a
On Mar 11, 8:35 am, John Harrison <john_androni...@hotmail.comwrote:
Dotani...@gmail.com wrote:
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
Thanks,
1. why it does'nt crush on the runtime phase?
2. can you show me the differ from c casting style & c++?

dotan

Mar 11 '07 #3

P: n/a
Do*******@gmail.com wrote:
On Mar 11, 8:35 am, John Harrison <john_androni...@hotmail.comwrote:
>>Dotani...@gmail.com wrote:
>>>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


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

>
dotan
Mar 11 '07 #4

P: n/a
On 3月11日, 下午1时39分, "Dotani...@gmail.com" <Dotani...@gmail.comwrote:
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
Mar 11 '07 #5

P: n/a
Do*******@gmail.com wrote:
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
Mar 11 '07 #6

P: n/a
Bo Persson wrote:
>
>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*.
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/
Mar 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.