Pointer to a member in C ++

Source: Internet
Author: User

Pointer to a member in C ++

A class has two basic types of members: function members and data members. Similarly, there are two types of pointers to members: pointers to function members and pointers to data members. It is not commonly used because classes generally do not contain public data members. They are used only to inherit the code written in C to coordinate the structure (struct) and Class (class).

Pointers to members are one of the most incomprehensible structures in C ++ syntax, but they are also the most powerful feature of C ++. It allows you to call a function member of a class without having to know the name of the function. This very agile calling tool. Similarly, you can use a pointer to a data member to check and change the data without having to know its member name.

Pointer to a data member

Although at the beginning, the syntax for pointing to the member pointer may confuse you a little bit, but you will soon find that it is actually similar to a common pointer, but it is more:: Symbol and class name. For example, define a pointer to the int type:

Int * PI;
Define a data member pointing to an int type class:
Int A: * PMI; // PMI is a member of the int type pointing to Class.
You can initialize it like this:
Class
{
Public:
Int num;
Int X;
};
/*
The pointer to a data member of a class is not the same as the initialization and Assignment Methods of a common pointer, because a common pointer assigns an actual object, such as Int J, during initialization or assignment; int * I = & J;
The pointer to the data member gives the address of a class member that does not exist yet, and this address is truly determined only when the actual class object is called, as follows:
*/
 
Int A: * PMI = & A: num;
The code above declares an int-type num member pointing to Class A and initializes it as the address of this Num member. by adding * Before PMI, you can use and change the value of the num member of Class:
A A1, A2;
Int n = A1. * PMI; // assign a1.num to n
A1. * PMI = 5; // assign 5 to a1.num
A2. * PMI = 6; // assign 6 to 6a2. Num.
 
If you define a pointer to Class A, you must use the-> * operator instead of the above operation:
A * pA = new;
Int n = pa-> * PMI;
Pa-> * PMI = 5;

Pointer to a function Member

It consists of the Data Types returned by function members. The class name follows the symbol, pointer name, and function parameter list. For example, a pointer to a function member of Class A (this function returns the int type:
 
Class
{
Public:
Int func ();
};
INT (A: * PMF )();
 
The above definition means that PMF is a pointer to the function member func () of Class. in fact, this pointer is no different from a common pointer to a function, but it contains the class name and: symbol. You can call this function in any place where * PMF is used.
Func ():
PMF = & A: func;
A;
(A. * PMF) (); // call A. func ()
If you first define a pointer to an object, the above operation should be replaced by->:
A * pA = &;
(Pa-> * PMF) (); // call pa-> func ()
The pointers to function members should consider polymorphism. Therefore, when you call a virtual function member through a pointer, this call will be dynamically recycled. You cannot take the address of the constructor and destructor of a class.

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.