Data member and function pointer pointing to the class

Source: Internet
Author: User

 

Pointer to a class member

In C ++, you can define a pointer to point to class members or member functions, and then use the pointer to refer to class members. This includes the pointer to the property member and the pointer to the member function.

À pointer to a data member

In C ++, you can define a pointer to point to class members. When the attribute members are static and non-static, the usage of pointers is also different. The Definition Format of the pointer to a non-static data member is as follows:

 
1. <data type> <class name >:* <pointer Name> [= & <class name >:< non-static data member>]

The pointer to a non-static data member must be associated with the class during definition and must be associated with a specific object during use.

 
2. <Class Object Name>. * <pointer to a non-static data member>

The definition and usage of pointers to static data members are the same as normal pointers. They do not need to be associated with classes or specific objects during use.

Assume that a class student has been defined, which has a non-static math member and a static member (Chinese,CodeDemonstrate how to define pointers to them.

 
1. Student S1;
2. Int Student: * PM = & Student: math; // point to non-static attributes
 
3. S1. * PM = 100; // set non-static attributes
 
4. int * Pc = & Student: Chinese; // point to Static Property
 
5. * Pc = 10; // set static attributes

Analysis: This example defines the pointer PC and PM, respectively pointing to the class's static data member math and non-static data member math. When accessing pm, class instances must be used for modification. When accessing a PC, there is no difference with a normal pointer.

ÀPointer to a member function

Defining a pointer to a non-static member function must be consistent with the member function in three aspects: the parameter list should be the same, the return type should be the same, and the type of the member function should be the same. The definition format is as follows:

 
1. <data type> (<class name >:* <pointer Name>) (<parameter list>) [= & <class name >:< non-static member function>]

The method to use a pointer to a non-static member function is the same as to use a pointer to a non-static data member. The format is as follows:

 
2. (<Class Object Name>. * <pointer to a non-static member function>) (<parameter list> );

The pointer pointing to a static member function is the same as a normal pointer. It does not need to be associated with a class during definition, and does not need to be associated with a specific object during use.

 
3. <data type> (* <pointer Name>) (<parameter list>) [= & <class name >:< static member function>]

If student has a non-static member function F1 and a non-static member function F2, the Code demonstrates the definition of function pointers pointing to them.

 
1. Student S1;
2. Float (Student: * pf1) () = & Student: F1; // pointer to a non-static member function
 
3. (S1. * pf1) (); // call a pointer to a non-static member function
 
4. Void (* pf2) () = & Student: F2; // pointer to a static member function
 
5. pf2 (); // call the static member function

Analysis: When pointing to a non-static member function, the class name must be used as a qualifier, and the class instance must be used as a qualifier. When pointing to a static member function, you do not need to use a class name as a qualifier.

 

Analysis:

Pointname = & classname: member;

Assign the address of the specified member in the class to the pointer variable. classname is the defined class name, while member is the data member name. Obviously, the compilation system does not allocate storage space for class names, so there is no absolute address. Therefore, this assignment is based on the offset of the member relative to the object where the class is located, that is, the relative address (the number of bytes from the start position ). The offset of any member in the class is a constant.

Because the value of this pointer variable is a relative address rather than an absolute address pointing to a data member in an object, you cannot use this pointer variable alone to access data members. For example, * pointname is not allowed. Because this pointer variable is not a member of the class, it can only access the public data members of the object. To access the private data member of an object, you must use the member function.

Example:

 
# Include <iostream> using namespace STD; Class point3d {public: point3d () {x = y = z =-1;} float sum () {return X + Y + z;} public: Float x, y, z; static float s ;}; float point3d: S = 100; int main () {cout <point3d: S <Endl; point3d p3d; float point3d: * PFM = & point3d: X; cout <p3d. * PFM <Endl; float * pfsm = & point3d: S; cout <* pfsm <Endl; float (point3d: * PFF) () = & point3d :: SUM; cout <(p3d. * PFF) () <Endl ;}

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.