The pointer to a class member variable is not a pointer.

Source: Internet
Author: User

Refer to the relevant chapter of <C ++ required knowledge>
"Pointer to a class member variable". This term contains the term "class member variables", but strictly speaking, the member variables here only refer to non-static member variables, this term also contains the term "Pointer,
But strictly speaking, it does not include an address, and its behavior is not like a pointer. Simply put, it is "pointer to a class member variable" not a pointer. although this term is confusing,
A group of variables of the same type can be abstracted as a "pointer to a variable". In the same way, you can abstract a group of class member variables of the same type into a "pointer to class member variables". The two are consistent.

If you are familiar with the declaration Syntax of regular pointers, It is not complicated to declare a "pointer to a class member variable:
Int * ip; // a pointer to the int variable
Int C: * ip; // a pointer to the int member variable in Class C
All you have to do is write one more classname: to limit which class the Pointer Points

A regular pointer contains an address. If this pointer is unreferenced, the object of this address will be obtained:
Int a = 12;
Int * ip address;
Ip = &;
* Ip = 0;
A = * ip;
However, a "pointer to a class member variable" does not contain an address. Simply put, it is actually the offset of a member variable in the class. of course, strictly speaking, because the C ++ standard and how to implement "pointer to class member variables"
Make any rule to say that "pointer to class member variable" is an integer offset is not necessarily correct, but most compilers do. next let's take a look at how the "pointer to a class member variable" is used?
# Include "stdafx. h"

Struct CPoint
{
Double x;
Double y;
};

Void Print (CPoint * point, double CPoint: * p)
{
Printf ("% f/n", point-> * p );
}

Int main (int argc, char * argv [])
{
CPoint pt;
Pt. x = 10;
Pt. y = 20;

Double CPoint: * p = NULL;

P = & CPoint: x;
Double x = pt. * p;
Print (& pt, p );

P = & CPoint: y;
Double y = pt. * p;
Print (& pt, p );
 
Int offset = (int &) p;

Return 0;
}
Double CPoint: * p = NULL;
This is the declaration of "pointer to class member variables", but it only has a CPoint:. This Pointer Points to the double type member variables in the CPoint class.
P = & CPoint: x;
This is a value assignment of "pointer to class member variable". Remember, it is an offset rather than an address. Therefore, you must use this static method. Here, you cannot assign values using the PT object with an address.
Double X = pt. * P;
This is an example of "pointer to a class member variable". Remember, the actual address must be available for the solution reference, because the address-specific object PT must be used for the solution reference ,. * The syntax is a bit weird, but I 'd rather split it into PT. and * P.
Printf ("% F/N", point-> * P );
This is also the solution reference of "pointer to class member variable", and. * Similarly, if we have a pointer to a cpoint, we must use-> * To dereference it, you can also split-> * into point-> and * P.
Int offset = (Int &) P;
Here, the "pointer to the class member variable" is directly converted to int, where offset = 8 happens to be the offset of the member variable Y in the cpoint class in the class, it verifies the saying that "pointer to class member variable" is an offset.
However, I still can't help but advise you not to use this offset directly. This is, after all, the details of the internal implementation of the compiler. There are too many people who like this hacker code and show it around, true "pointer to class member variables"
Only include declaration, value assignment, and reference.

What about derivation?
# Include "stdafx. H"

Struct cpoint
{
Double X;
Double Y;
};

Struct cpoint3d: Public cpoint
{
Double Z;
};

Void Print (CPoint * point, double CPoint: * p)
{
Printf ("% f/n", point-> * p );
}

Int main (int argc, char * argv [])
{
CPoint pt;
Pt. x = 10;
Pt. y = 20;

Double CPoint: * p = NULL;

P = & CPoint: x;
Double x = pt. * p;
Print (& pt, p );

P = & CPoint: y;
Double y = pt. * p;
Print (& pt, p );

Int offset = (int &) p;

Double CPoint3d: * p3d = NULL;
P3d = & CPoint3d: z;

Offset = (int &) p3d;

P3d = p; // correct
// P = p3d; // Error
P = (double CPoint: *) p3d; // force conversion

CPoint3d pt3d;
Pt3d. z = 30;
Print (& pt3d, (double CPoint: *) p3d );

Return 0;
}

Offset = (int &) p3d;
Here, offset = 16 is the offset of the member variable z in CPoint3d in the class. Does anyone say offset = 0? Check the object model of C ++ again.
P3d = p; // correct
There is an implicit conversion from "pointer to class member variable" to "pointer to class member variable" in the derived class, it means that the member variable offset of the base class is only a subset of the member variable offset in the derived class,
Therefore, there should be no problem with this conversion, but what is the opposite?
// P = p3d; // Error
There is no implicit conversion from the "pointer to class member variables" of the derived class to the "pointer to class member variables" of the base class, because the member variables in the derived class cannot be found in the base class.
"Pointer to class member variable" Relationship between base class and derived class and "pointer to Class Object" the relationship between base class and derived class is completely opposite, this is reasonable in nature as to the "pointer to a class member variable", but in this case,
We can't use the public Print function unless...

P = (double CPoint: *) p3d; // force conversion
We can perform forced conversion.
CPoint3d pt3d;
Pt3d. z = 30;
Print (& pt3d, (double CPoint: *) p3d );
In addition, only the forced conversion can use the public print function. The print output here is 30. There is no error, because we pass in a pointer to a cpoint3d object, there is no error in the offset of the member variable.
But do you have to do this? It depends on the programmer's choice.

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.