A solution to C + + pointer drift problem

Source: Internet
Author: User
Tags object model printf

Although the C + + programming language is powerful and flexible in application, there are also various errors in the actual programming. Here we will give you a detailed description of the C + + pointer drift solution, I hope this article describes the content can help you solve the problem.

We have recently encountered a strange problem in our work, and we finally determined that the C + + pointer drift caused by multiple inheritance is related to the C + + object model. The signal is as follows:

Class A {...}; Class b{...}; Class Ab:public B, public A {...} ...} AB *pab = new AB (); A * PA = (A *) PAB; b* PB = (b*) PAB; This is when you find that the value of PA and PB is not the same! One of them is equal to the PAB and the other has an offset. If you change the order of A and B in AB's statement, the pointer that produces the offset becomes another.

To make sure that this is the compiler doing the conversion, use the void pointer to fool the compiler:

void *PV = (void*) PAB; PA = (A *) PV; The value of the PA is the same as the PAB, but it points to the wrong place. The conversion from PAB to PA relies on the choice of paths that are not very reassuring. Do not know to put the pointer into the container and then remove it, will not be wrong. Of course, the above uses mandatory type conversions, which should be avoided in good programs. If you have an implicit conversion, you can get the correct result:

Std::vector v; Implicit type conversion V.insert (V.begin (), PAB); void *PV = v[0]; PA = (A *) PV; The following programs are compiled by using cygwin/g++b:

#include #include Class A {public:int A;}; Class B {Public:int B;}; Class Ab:public B, public A {public:int AB; int main (int argc, char **argv) {AB *pab = new AB (); pab->ab = 1; pab->b = 2; pab->a = 3; A * PA = (A *) PAB; b* PB = (b*) PAB; printf ("AB:%pn" "A:%pn" "B:%pn", PAB, PA, Pb); Std::vector v; Implicit type conversion V.insert (V.begin (), PAB); void *PV = v[0]; PA = (A *) PV; printf ("PV is%pnpa is%pnpab%s PVN", PV, PA, (PAB = PV)? "==" : "!="); printf ("A.A is%dn", pa->a); Forced type Conversion PV = (void*) PAB; PA = (A *) PV; printf ("Now A.A is%dn", pa->a); Run Results:

ab:0x6b01f0 a:0x6b01f4 b:0x6b01f0 PV is 0x6b01f4 pa are 0x6b01f4 PAB!= PV a.a is 3 now A.A is 2

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.