C ++ pointer drift Solution

Source: Internet
Author: User

Although the C ++ programming language has powerful functions and flexible application methods, various errors may also occur in actual programming. Here we will introduce in detail how to solve the C ++ pointer drift. I hope this article will help you solve the problem.

Recently we encountered a strange problem in our work. We finally decided that the C ++ pointer drift caused by multi-inheritance is related to the C ++ object model. The diagram is as follows:

Class {...}; class B {...}; class AB: public B, public {...}... AB * pab = new AB (); A * pa = (A *) pab; B * pb = (B *) pab; at this time, you find that the values of pa and pb are different! One of them is equal to pab, And the other generates an offset. If we change the order of A and B in the AB statement, the pointer that generates the offset will change to another one.

To make sure that this is the reason for the compiler to convert, use the void pointer to fool the compiler:

Void * pv = (void *) pab; pa = (A *) pv; at this time, the value of pa is equal to that of pab, but points to the wrong place. The transition from pab to pa depends on the path selection, which is not reassuring. I still don't know if it will make an error if I put the pointer into the container and then retrieve it. Of course, forced type conversion is used above and should be avoided in good programs. If there is only implicit conversion, the correct result is displayed:

Std: vector v; // implicit type conversion v. insert (v. begin (), pab); void * pv = v [0]; pa = (A *) pv; the following program is compiled 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" ": % 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 % pnp A is % pnpab % s pvn ", pv, pa, (pab = pv )? "= ":"! = "); Printf (". a is % dn ", pa-> a); // forced type conversion pv = (void *) pab; pa = (A *) pv; printf (" Now. a is % dn ", pa-> a);} running result:

AB: 0x6b01f0 A: 0x6b01f4 B: 0x6b01f0 pv is 0x6b01f 4 Pa is 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.