A pointer problem for C + + multiple inheritance

Source: Internet
Author: User

Here are some questions about pointers in C + + multiple inheritance.

pointer pointing problem
First look at the following program:

classbase1{ Public:Virtual voidFun1 () {cout<<"Base1::fun1"<< Endl;};};classbase2{ Public:Virtual voidFun2 () {cout<<"Base2::fun1"<< Endl;};};classDerive: PublicBASE1, Publicbase2{ Public:Virtual voidFun1 () {cout<<"Derive::fun1"<< Endl;}Virtual voidFun2 () {cout<<"Derive::fun2"<< Endl;}};intMain () {Derive OD;    Base1 *PB1 = (base1*) (&AMP;OD); Base2 *PB2 = (base2*) (&AMP;OD);cout<<"&od="<< &od <<' \ n ';cout<<"pb1="<< pB1 <<' \ n ';cout<<"pb2="<< pB2 <<' \ n ';if(&od = = pB1)cout<<"&od = = pB1"<<' \ n ';if(&od = = pB2)cout<<"&od = = pB2"<<' \ n ';}

Running results on my computer:

First, you can see that the &od and pB1 pointers point to the same storage address. Why?
This is because when we new a derive class, the space allocated by the computer to the derive class can be divided into three parts: first the part of the class Base1, then the Base2 part, and then derive the remainder of the base and Base2, for example.

Base1
Base2
Derive

So &od is sure to save the whole of the first address, and PB1 Point is Base1 's first address, is also the whole of the first address, so there are &od and pB1 values just equal. PB2 points to the first address of BASE2.
But why is there &od = = pB2? This is because when the compiler discovers a pointer to a derived class and a pointer to its base class for the = = operation, the pointer is automatically implicitly typed to promote the pointer differences that are masked by multiple inheritance. Because two pointers are compared, the goal is usually to determine whether two pointers point to the same memory object instance, in the above scenario, &od and pB2, although the pointer values are not equal, but they really point to the same memory object (that is, the new derive generated memory objects).

pointer type conversion issues
Or use the class above to see the main function:

intMain () {Derive OD;cout<<"&od="<< &od <<' \ n '; Base1 *PB1 = &oD;cout<<"pb1="<< pB1 <<' \ n '; Pb1->fun1 ();cout<< Endl; Base2 *PB2 = (base2*) (pB1);//pointer forced conversion, no offset    cout<<"pb2="<< pB2 <<' \ n '; Pb2->fun2 ();cout<< Endl; PB2 =dynamic_cast<Base2*> (pB1);//Pointer dynamic conversion, dynamic_cast help you offset    cout<<"pb2="<< pB2 <<' \ n '; Pb2->fun2 ();return 0;}

Guess the execution result:

It is not surprising, why pb2->fun2 () The result is derive::fun1. What we see here is that using a forced type conversion is a pointer to a BASE2 type that cannot be converted to a Base1 type, and must be converted in the form of dynamic_cast to work.

Below we explore why the output is derive::fun1.
We modify the definition of Base1:

class Base1{public:    virtualvoid fun3() {cout"Base1::fun3" << endl;};    virtualvoid fun1() {cout"Base1::fun1" << endl;};};

To add a function fun3, and then execute the above main function again, the result is as follows:

We can see that the cast will not succeed or error, you call Base2 's fun2 function, because the cast is unsuccessful, so the pointer still points to BASE1, and Base1 does not have fun2, so the first function in the declared function is called automatically. (Do not know why this design!) )

Forcing the BASE1 to Base2 will not be an error, but the correct result cannot be run. And we're forcing Base2 to Base1?

int main(){    *pD = new Derive();    *pB2 = pD;    pB2->fun2();    *pB1 = (Base1*)(pB2);    pB1->fun1();    return0;}

This way the program executes to line 6th and crashes directly.
So:

1.C++多重继承需要慎用2.类型转换尽量采用c++内置的类型转换函数,而不要强行转换。

A pointer problem for C + + multiple inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.