C + + Learning Notes (iv)--pointer (3) __c++

Source: Internet
Author: User
Tags static class

C + + use of pointers to class members (details)

1. First, ordinary function pointers cannot be assigned to the address of a member function, even if the return type and parameter exactly match. For example: The following is the PFI is a normal function pointer, it has no parameters, the return type is int:

Int (*PFI) ();

If there are two global functions, Heightis () and Widthis ():

int Heightis ();

int Widthis ();

The following assignment is legal:

PFI = Heightis ();

PFI = Widthis ();

But now a class screen also defines two access functions-height () and width (), and they have no parameters,

The return type is also int:

inline int screen::height () {return _height;}

inline int screan::width () {return _width; }

However, the following assignment is illegal and can cause compilation errors to occur.

PFI = &screen::height ();

Why there is an offence. Because a member function has a property that a non-member function does not have-its classes (class).

A pointer to a member function must match the function type that it assigns, not two aspects but three aspects:

(1) Parameter type and number (2) return type (3) the class type to which it belongs.

The mismatch between the member function pointer and the normal function pointer is due to the difference in representation between the two pointers. function pointers

The address of the stored function that can be used to call that function directly. member function pointer head

You must bind to an object or a pointer before you can get the this pointer of the called object and then tune

Use the member function that the pointer refers to. You'll see how the member function pointers are bound to an object or pointer below.

To invoke a member function.

2. Declaration of member function pointers:

Take the following class to illustrate:

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://correction

String _screen;

String::size_type _cursor;

Short _height;

Short _width;

};

The declaration of a member function pointer requires an extended syntax, which takes into account the type of the class. A pointer to a class data member is also

That's it. Consider the type of the member _height of the screen class. Its complete type is "the short type of screen class

The full type of the pointer to the _height is a pointer to a member of the Type screen class that is short.

Write as:

Short screen:*

The pointer to the members of the short Type screen class is defined as follows:

Short screen ::* ps_screen;

Ps_screen can be initialized with a _height address as follows

Short screen::* Ps_screen = &Screen::_height;

The mismatch between the data member pointer and the normal pointer is also due to the difference in the representation of the two pointers. Ordinary means

The needle contains all the information needed to refer to an object. Before a data member pointer is used to access a data member, it must first be

Bound to an object or pointer.

Defining a member function pointer requires specifying the function return type, parameter table, and class. For example, point to screen member letter

The pointer types that count and can reference the member function height () and width () are as follows:

Int (screen::*) ();

This type specifies a pointer to the member function of Class screen, which has no arguments, and the return value type is int.

Pointers to member functions can be declared, initialized, and assigned as follows

All pointers to class members can be assigned with a value of 0

Int (screen::* pmf1) () = 0;

Int (screen::* pmf2) () = &Screen::height;

PMF1 = PMF2;

PMF2 = &Screen::width;

You can also use a TypeDef definition so that the syntax is easier to read. Such as:

typedef screen& (screen::* Action) ();

Action default = &Screen::home;

Action next = &Screen::forward;

3. How to use pointers to class members

A pointer to a class member must always be accessed through a specific object or a pointer to a modified object. is through

Use two pointers to member operators (for class objects and references, and->* for pointers to class objects)

(Operators. * and->* are described below:

Pm-expression:

Cast-expression

Pm-expression. * Cast-expression

Pm-expression->* cast-expression

The binary operator. * combines its operand, which must is an object of class type,

With its second operand, which must to be a pointer-to-member type.

The binary operator->* combines its I-operand, which must is a pointer to an object

of class type, with its second operand, which must to be a pointer-to-member type.

In an expression containing the. * operator, the the ' the ' of the ' the ' the ' the '

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 the ' the ' the ' the ' operand ' of the ' type ' must

To the class type ' of the type specified in the second operand, or it must being of a type

unambiguously derived from that class.

)

As in the following example:

Int (screen::* pmfi) () = &Screen::height;

screen& (screen::* PMFs) (const screen&) = &Screen::copy;

Screen Myscreen, *bufscreen;

Calling member functions directly

if (myscreen.height () = = Bufscreen->height ())

Bufscreen->copy (Myscreen);

An equivalent call through a member pointer

if ((MYSCREEN.*PMFI) () = = (Bufscreen->*pmfi) ())

(BUFSCREEN->*PMFS) (Myscreen);

Pointers similar 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 members of the pointer:

There is a difference between pointers to non-static class members and guidelines for static class members. Pointer syntax for class members is not

Static member static Class members that can be used to refer to a class. is a global object and function that belongs to the class. Their pointers are normal

Pointer.

Such as:

Class a{

Public:

static void f ();

Public

static int m_data;

};

The definition of pointing to the m_data pointer is as follows:

int *p = &A::m_data;

Error

int A::* p = &A::m_data;

Pointing to the F function pointer can be defined as this: it is a normal function pointer

void (*PTRF) = &A::f;

Source Documents

 

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.