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
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