Item 15. Pointer to class data members

xiaoxiao2021-03-05  46

Item 15. Pointers to Class Members Are Not Pointers

Pointers to Class Members: Pointer to Pointer

Note: This section only involves non-static data members -------------------------------------- ------------------ 1, the statement of the pointer of the class: Class C {public: // ... int A_;}; int C :: * PIMC; / / Point to the pointer of the class C, here is just a statement

2. The meaning of the pointer of the class member: The pointer of the class member is easy to cause confusion because the meaning of the pointer of the class member and the meaning of the pointer: the pointer contains the address; and the pointer of the class member does not involve Memory address, it just references a member of a class, not a member of a specific object, there is no memory address, so the pointer of the class member cannot point to some place in memory, which is usually just an offset Ofset. However, since standard C does not define the implementation of the pointer of the class member, the implementation of each compiler manufacturer is not the same, but most of them use an integer to represent this offset.

PIMC = & C :: A_; // Gets the offset of member A_ in C, if A_ is a static member, PIMC points to the address 3 of C :: A_, and access the data member C AC corresponding to the offset. C * pc = & ac; ac. * PIMC = 0; // * PIMC Application to INT b = PC -> * PIMC; has a class member's offset, in order to get this offset The data member on the quantity, we need the address of the object of the class. So, ". *" And "-> *" debuted. AC. * PIMC Meaning: Access Object AC In the offset of data member PC -> * PIMC's meaning: Access the pointer PC pointing to data members on the offset of PIMC

4, when inheriting ... In the inheritance system, the pointer of the subclass object can implicit a pointer to the bit parent class object, and the conversion is required. However, the pointer conversion rules for class members are just the opposite. Don't be strange, everyone follows the same rules: the children of the parent are certain, and the parent classes of the child do not necessarily have. Class shape {// ... point center_; //... }; Class Circle: Public Shape {// ... Double Radius_; // ...};

Point circle :: * Loc = & shape :: center_; // ok // double shape :: * eXtent = & circle :: radius_; // error!

转载请注明原文地址:https://www.9cbs.com/read-36555.html

New Post(0)