Usage of C ++ pointer to class member (details)

Source: Internet
Author: User
[Switch] usage of C ++ pointer to class member (details)

Default category: 20:39:57 read 176 comments 0 font size: LargeMediumSmall subscription

1. First, the normal function pointer cannot be assigned as the address of the member function, even if the return type and Parameter Match completely. For example, the following PFI is a normal function pointer, which has no parameters and the return type is int:
INT (* PFI )();
If there are two global functions, heightis () and widthis ():
Int heightis ();
Int widthis ();
The following assignment operation is legal:
PFI = heightis ();
PFI = widthis ();

But now there is a class screen that also defines two access functions-height () and width (), and they do not have parameters,
The return type is also INT:
Inline int screen: height () {return _ height ;}
Inline int screan: width () {return _ width ;}
However, the value assignment below is invalid, which may cause compilation errors.
PFI = & screen: height ();

Why is there a violation? This is because a member function has a class attribute that does not belong to a non-member function ).
The pointer to a member function must match the function type assigned to it, not in two aspects, but in three aspects:
(1) parameter type and number (2) return type (3) class type to which it belongs.

The mismatch between the member function pointer and the common function pointer is caused by the difference in the two pointers. Function pointer
The address of the stored function, which can be used to directly call the function. Member function pointer Header
You must be bound to an object or a pointer to obtain the this pointer of the called object before calling
A member function referred to by a pointer. The following shows how the member function pointer is bound to an object or pointer,
To call a member function.

2. Declaration of member function pointers:
Use the following class to describe:
Class Screen {
Public:
// Member functions
Void home () {_ cursor = 0 ;}
Void move (INT, INT );
Char get () {return _ screen [_ cursor];}
Char get (INT, INT );
Bool checkrange (INT, INT );
Int height () {return _ height ;}
Int width () {return _ width ;}
//....
Public: // corrected
String _ screen;
String: size_type _ cursor;
Short _ height;
Short _ width;
};

The Declaration of the member function pointer requires the extended syntax, which should consider the type of the class. The pointer to the class data member is also
Yes. Consider the member _ height type of the screen class. Its complete type is
The complete type of pointer pointing to _ height is the pointer pointing to a member of the Short-Type Screen class.
Written:
Short screen :*
The pointer to a member of the Short-Type Screen class is defined as follows:
Short screen: * ps_screen;
Ps_screen can be initialized with the _ height address as follows:
Short screen: * ps_screen = & screen: _ height;

The mismatch between the data member pointer and the common pointer is also caused by the difference in the expression of the two pointers. Normal
The needle contains all the information required to reference an object. The data member pointer must be
Bound to an object or pointer.

To define a member function pointer, you must specify the function return type, parameter table, and class. For example, to the screen member letter
The pointer types that can reference the member functions height () and width () are as follows:
INT (screen ::*)();
This type specifies a pointer to a member function of the screen class. It has no parameter and the return value type is int.
The pointer to a member function can be declared. Initialization and assignment are as follows:
// All pointers to class members can be assigned a value of 0.
INT (screen: * pmf1) () = 0;
INT (screen: * pmf2) () = & screen: height;
Pmf1 = pmf2;
Pmf2 = & screen: width;
You can also use typedef to define the syntax, which is easier to read. For example:
Typedef screen & (screen: * Action )();
Action default = & screen: home;
Action next = & screen: forward;

3. How to Use pointers to class members
Class member pointers must always be accessed through a specific object or a pointer to a modified object. Yes
Use two pointers to the member operator (for class objects and referenced. *, and for pointers to class objects-> *)
(Operators. * And-> * are described as follows:
PM-expression:
Cast-expression
PM-expression. * Cast-expression
PM-expression-> * Cast-expression

The binary operator. * combines its first operand, which must be an object of class type,
With its second operand, which must be a pointer-to-member type.

The binary operator-> * combines its first operand, which must be a pointer to an object
Of class type, with its second operand, which must be a pointer-to-member type.

In an expression containing the. * operator, the first operand must be of the class type
Of the pointer to member specified in the second operand or of a type unambiguously derived
From that class.

In an expression containing the-> * operator, the first operand must be of the type "pointer
To the class type "of the type specified in the second operand, or it must be of a type
Unambiguously derived from that class.

)

For example:
INT (screen: * pmfi) () = & screen: height;
Screen & (screen: * pmfs) (const screen &) = & screen: copy;
Screen myscreen, * bufscreen;
// Directly call the member function
If (myscreen. Height () = bufscreen-> height ())
Bufscreen-> copy (myscreen );
// Use the member pointer's equivalent call
If (myscreen. * pmfi) () = (bufscreen-> * pmfi )())
(Bufscreen-> * pmfs) (myscreen );

Pointers pointing to data members can be accessed in the following ways:
Typedef short screen: * ps_screen;
Screen myscreen, * tmpscreen = new screen (10, 10 );
Ps_screen pH = & screen: _ height;
Ps_screen PW = & screen: _ width;
Tmpscreen-> * pH = myscreen. * pH;
Tmpscreen-> * PW = myscreen. * PW;

4. Static class member pointers:
There is a difference between the pointer of a non-static class member and the guidance of a static class member. The pointer syntax to the class member is not
Static class members that can be used to reference a class. Are global objects of this class and their pointers are normal
Pointer.

For example:
Class {
Public:
Static void F ();
Public:
Static int m_data;
};

The m_data pointer is defined as follows:
Int * P = & A: m_data;

// Error
Int A: * P = & A: m_data;
The pointer to the f function can be defined as follows: it is a common function pointer.
Void (* ptrf) () = & A: F;

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dremi/archive/2007/11/16/1888635.aspx

Related Article

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.