Home > Back-end >  C inherited problem
C inherited problem

Time:10-26

The program is like this:
Class A, {}.
Class B {};
Class C: public A, public {B};
Void main ()
{
C * C=new C;
B * B=static_cast & lt; B * & gt; (c);
The delete b;
}
Compile-time error, if you will be the order of A and B when inheritance after switch can run normally, and, if is to inherit A, B C is inherited, can run normally, why is this so, strives for the great god,

CodePudding user response:

When multiple inheritance first compare the main,

CodePudding user response:

When multiple integration of several base class in memory location is not the same, one of the first in the front, address the same as the derived classes, so when the delete will not go wrong

CodePudding user response:

On the second floor, can you point in detail, it is not clear

CodePudding user response:

Take a look at this link, should speak more detail
http://blog.csdn.net/dongfengsun/article/details/1476362

CodePudding user response:

Dataxdata, thanks for sharing!

CodePudding user response:

C * C=new C;
B * B=static_cast & lt; B * & gt; (c);//this point is not equal to c to b, "probably" will have an offset, which is connected with the compiler,
The delete b;//cause the abnormal, moreover, generally don't use, unless for virtual destructor,

Multiple inheritance is very trouble, I also don't understand,

CodePudding user response:

 
B * B=new C;
The delete b;

CodePudding user response:

Pointed out this is because b c b type in the way a \ b refers to the first address is not c, so there is something wrong with the release,

CodePudding user response:

refer to the original poster xlq1204 response:
program is like this:
Class A, {}.
Class B {};
Class C: public A, public {B};
Void main ()
{
C * C=new C;
B * B=static_cast & lt; B * & gt; (c);
The delete b;
}
Compile-time error, if you will be the order of A and B when inheritance after switch can run normally, and, if is to inherit A, B C B to inherit, also can...

Delete a base class pointer; Such operations to delete a derived class object, then the base class destructor must be virtual functions,
Or even run no error, that is the wrong code,

 class A {}; 
Class B {public: virtual ~ (B) {}};
Class C: public A, public {B};
Int main ()
{
C * C=new C;
B * B=c;
The delete b;
}

CodePudding user response:

Inheritance to see whether the virtual function table,
  • Related