C + + Primer Learning notes _103_ special tools and techniques--class member pointers

Source: Internet
Author: User
Tags aliases

Special tools and techniques--class member pointers

A member pointer can do this: get a pointer to a specific member , and then get that member from an object or another object . The member pointer should contain the type of the class and the type of the member .

One.declaring member Pointers

Test class :

Class Screen{public:    typedef std::string::size_type Index;    Char get () const;    Char Get (Index Ht,index wd) Const;private:    std::string contents;    index cursor;    index height,width;};

1. Defining data member pointers

Contentsthe exact type is"a member of the screen class whose type is std::string ”. Therefore, you can point toContentsthe exact type of the pointer is"Pointingstd::stringtype of ScreenPointer to a class member", you can point to ScreenClass ofstringA pointer to a member is defined as

std::string screen::* Ps_screen;

can be initialized with contents address Ps_screen

std::string screen::* Ps_screen = &Screen::contents;

You can also define a pointer to a height,width or cursor member as :

Screen::index screen::* pindex;

This means thatpindex is a pointer to a member of the screen class with the screen::index type . You can Assign the width address to this pointer

Pindex = &screen::height;pindex = &Screen::width;


2. Pointers for defining member functions

Defines a pointer to a member function by specifying the function return type, formal parameter list, and class. For example, a pointer to a screen member function that does not accept a get version of a formal parameter has the following type :

char (screen::*) () const;

The pointer to the GET function version can be defined and initialized as follows :

char (screen::* PMF) () const = &Screen::get;

You can also define a pointer to a get function version with two parameters as

char (screen::* pmftworef) (screen::index,screen::index) Const;pmftworef = &Screen::get;

[Be careful !]

The invocation operator takes precedence over the member pointer operator, so bracketing screen: The parentheses of the :* are necessary, and without the parentheses, the compiler treats the following code as (invalid) function declaration:

     Error:non-member function P cannot has a const qualifier      char screen::* p () const;


3. Use type aliases for member pointers

Type aliases make the member pointers easier to read.

    typedef char (screen::* Action) (screen::index,screen::index) const;

Action is a pointer to a type " screen class that accepts two index type parameters and returns a member function of char " 's name. Using type aliases, you can simplify the definition of get pointers to

Action get = &Screen::get;

You can also use the member pointer function type to declare the function parameters and the return type of the function :

Screen &action (Screen &,action = &screen::get);

You can call the action function by passing a pointer or address to the appropriate member function in the screen class :

Screen myscreen;action (myscreen); action (myscreen,get); action (myscreen,&screen::get);

Two.using pointers to class members

Similar to the member access operator . and the - , .*  and the - is two new operators that enable us to bind member pointers to actual objects. The left operand of both operators must be a pointer to the class type's object or class type, and the right operand is a member pointer of that type.

? member pointers dereference operators (. *) gets a member from an object or reference.

? member pointer arrow operator (->*) gets a member from a pointer to an object.

1. Pointers using member functions

    char (screen::* PMF) () const = &Screen::get;    Screen Myscreen;    Char ch1 = Myscreen.get ();    Char CH2 = (MYSCREEN.*PMF) ();   Equivalent call to get screen    *pmyscreen = &myScreen;    CH1 = Pmyscreen, get ();    CH2 = (Pmyscreen->* PMF) ();    Equivalent call to get    //Use version with Parameters    char (screen::* pmftworef) (screen::index,screen::index) const = & Screen::get;    CH1 = Myscreen.get (0,0);    CH2 = (myscreen.*pmftworef) (0,0);   Equivalent call to get

[Be careful !]

Call ( MYSCREEN.*PMF) () and (pscreen-) are called because the call operator (()) has a higher precedence than the member pointer operator. >*PMF) () requires parentheses .

2. Using pointers to data members

    Screen::index screen::* pindex = &Screen::width;    Screen Myscreen;    Screen::index index1 = myscreen.width;    Screen::index index2 = Myscreen.*pindex;    Screen *pmyscreen = &myScreen;    index1 = pmyscreen width;    Index2 = Pmyscreen->* pindex;

3. Member Pointer function table

A common use of function pointers and member function pointers : store them in a function table . A function table is a collection of function pointers , Select a given call from at run time .

For a class that has several members of the same type, you can use such a table to select one from the collection of these members. such as :

Class Screen{public: Screen    &home ();    Screen &forward ();    Screen &back ();    Screen &up ();    Screen &down ();p rivate:    //...};

None of these new functions accept the formal parameter and return a reference to the object that called it .

4. Using the function pointer table

We may want to define a move function that can call any of these functions and perform the specified action. To support this new function, we will Add a static member to the screen that is an array of pointers to the cursor movement function :

Class Screen{public://as before    typedef    & (screen::* Action) ();    Static Action menu[];//function table public:    enum Directions {home,forward,back,up,down};//decides which direction to move screen    &move (directions);p rivate:    //as before};

An array named Menu will hold pointers to each cursor movement function , and those functions will be saved at an offset corresponding to the enumeration member in directions , the move function takes an enumeration member and invokes the appropriate function :

screen& Screen::move (directions cm) {    (this->* menu[cm]) ();    return *this;}

This computes the call inside the move : gets the Menu element indexed by cm ( the element Division Screen pointers to class member functions ), which represents the member function that this element points to when the object that this is pointed to is called.

Screen Myscreen;myscreen.move (screen::home); Myscreen.move (screen::up);

5. Define the member function pointer table

Define and initialize the table itself screen::action screen::menu[] ={    &screen::home,    &screen::forward,    &screen:: Back,    &screen::up,    &screen::d own};

P657 Exercise 18.26~27    screen::index screen::* pcursor = &Screen::cursor;    typedef    CHAR (screen::* pgetnoref) () const;    typedef    CHAR (screen::* pgettworef) () const;    typedef screen    & (screen::* pmovenoref) ();    typedef screen    & (screen::* pmovetworef) ();

Exercise 18.28~31 class definition is as follows class Screen{public:    typedef std::string::size_type Index;    typedef    screen& (screen::* pfunnoref) ();    typedef screen    & (screen::* Action) ();    Char get () const;    Char Get (Index HT,INDEX wd) const;    Screen &home ();    Screen &forward ();    Screen &back ();    Screen &up ();    Screen &down ();p rivate:    static Action menu[];p ublic: Screen    (pfunnoref RF = &screen::home):p MF (RF) { }    void Setpmf (pfunnoref rf = &screen::home)    {        PMF = RF;    }    Enum directions {Home,forward,back,up,down};    Screen &move (directions);p rivate:    std::string contents;    index cursor;    Index height,width;public:    pfunnoref PMF;};

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.