C ++ Primer study note _ 103 _ special tools and technology, primer_103

Source: Internet
Author: User

C ++ Primer study note _ 103 _ special tools and technology, primer_103
Special tools and technologies-class member pointers

 

The member pointer can be used to obtain the pointer of a specific member, and then obtain the member from an object or another object. The member pointer should contain the class type and the member type.

 

I. Declare a member pointer

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. Define the data member pointer

The full type of contents is "Screen class member, whose type is std: string ". Therefore, the full type of pointer to contents is "pointer to Screen class members of std: string type", and the pointer to string members of Screen class can be defined

std::string Screen::*ps_Screen;

You can use the contents address to initialize ps_Screen.

std::string Screen::*ps_Screen = &Screen::contents;

You can also define the pointer of the height, width, or cursor member:

Screen::index Screen::*pindex;

 

This means that pindex is a pointer to a Screen class member with the Screen: index type. You can assign the width address to this pointer.

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


2. Define the pointer of a member function

Define the pointer of a member function by specifying the function return type, parameter table, and class. For example, the pointer to the Screen member function that can reference get versions that do not accept the form parameter has the following types:

char (Screen::*)() const;

The get function version pointer can be defined and initialized as follows:

char (Screen::*pmf)() const = &Screen::get;

You can also define the get function version with two parameters

char (Screen::*pmfTwoRef)(Screen::index,Screen::index) const;pmfTwoRef = &Screen::get;

[Be careful!]

The call operator has a higher priority than the member pointer operator. Therefore, parentheses surrounding Screen: * are necessary. Without such parentheses, the compiler treats the following code as (invalid) function declaration:

     // error: non-member function p cannot have const qualifier      char Screen::*p() const;


3. Use a type alias for the member pointer

Type aliases make Member pointers easier to read.

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

Action is the name of the type "the Screen class accepts two index type parameters and returns the pointer of the char member function. You can use a type alias to simplify the get pointer definition

Action get = &Screen::get;

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

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

You can call the action Function by passing the pointer or address of the appropriate member function in the Screen class:

Screen myScreen;action(myScreen);action(myScreen,get);action(myScreen,&Screen::get);

 

2. Use pointers of Class Members

Similar to the member access operator. And->,. * And-> are two new operators that enable us to bind member pointers to actual objects. The left operands of these two operators must be class objects or class pointers, and the right operands are member pointers of this type.

• The member pointer unreferencing operator (. *) gets a member from an object or reference.

• The member pointer arrow operator (-> *) obtains the member through the object pointer.

 

1. Use the pointer of the member function

Char (Screen: * pmf) () const = & Screen: get; Screen myScreen; char Screen = myScreen. get (); char ch2 = (myScreen. * pmf) (); // equivalent call to get Screen * pMyScreen = & myScreen; success = pMyScreen-> get (); ch2 = (pMyScreen-> * pmf )(); // equivalent call to get // use the char (Screen: * pmfTwoRef) (Screen: index, Screen: index) const = & Screen: get; screen = myScreen. get (0, 0); ch2 = (myScreen. * pmfTwoRef) (0, 0); // equivalent call to get

 

[Be careful!]

Because the call operator () has a higher priority than the member pointer operator, brackets are required for calling (myScreen. * pmf) () and (pScreen-> * pmf.

 

2. Use the pointer of the data member

    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: stores them in the function table. A function table is a collection of function pointers and selects a given call during runtime.

For classes with several members of the same type, you can use such a table to select one from the set of these members. For example:

class Screen{public:    Screen &home();    Screen &forward();    Screen &back();    Screen &up();    Screen &down();private:    //...};

These new functions do not accept Form parameters and return references to the objects that call them.

 

4. Use the function finger table

We may want to define a move function, which can call any of these functions and execute the specified action. To support this new function, we will add a static member to Screen, which is an array of pointers of the cursor moving function:

Class Screen {public: // As Before typedef Screen & (Screen: * Action) (); static Action Menu []; // function table public: enum Directions ctions {HOME, FORWARD, BACK, UP, DOWN}; // determines where to move Screen & move (Directions); private: // As Before };

 

The array named Menu will save the pointer pointing to each cursor moving function, and save those functions at the offset corresponding to the enumerated member in Directions. The move function accepts the enumerated member and calls the appropriate function:

Screen& Screen::move(Directions cm){    (this ->* Menu[cm])();    return *this;}

Calculate the internal call of move: Obtain the Menu element indexed by cm (pointer of the Screen class member function of this element), which indicates that this points to the object and calls the member function pointed to by this element.

Screen myScreen;myScreen.move(Screen::HOME);myScreen.move(Screen::UP);

5. Define the member function finger table

// Define and initialize the table itself. Screen: Action Screen: Menu [] ={ & Screen: home, & Screen: forward, & Screen: back, & Screen:: up, & Screen: down };

// 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 ~ Class 31 is defined 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 (); private: static Action Menu []; public: Screen (pFunNoRef rf = & Screen: home ): pmf (rf) {} void setPmf (pFunNoRef rf = & Screen: home) {pmf = rf;} enum Directions ctions {HOME, FORWARD, BACK, UP, DOWN }; screen & move (Directions); private: std: string contents; index cursor; index height, width; public: pFunNoRef pmf ;};


C programming language and C Primer Plus which is better?

The former is only an entry-level book. If you want to use C entry-level programming, you can read this book and learn C ++ or JAVA in the future. C primer plus is the most comprehensive .. If you want to develop in C, you can see that he is also a tool book. If you want to study C in depth, you need to read more books at the bottom of C, such as C compiler implementation. C language Assembly knowledge

After learning c primer plus, what should I learn?

I have read a lot of books from the author. The advantage is that the knowledge is wide and the disadvantage is that it is easy to be refined. I personally strongly suggest:
Clear goals-clear programming language-select a good book for good reading, good programming (manual programming is the focus)-select a lot of books (the same programming language) as a tool book-start to achieve the goal

In addition, if you only want to practice applications (such as Enterprise Portal and office platform websites), Learning data structures is of little use. The application algorithm has already been saved to the relevant class library. If you are not interested in it (Love coding algorithm, curious ...), The help is really not too great.
Assembly language .. First, it is not practical for window design .. It is hard to understand and use. If you do not want to do embedded-related things, you really don't need it. If you are really interested, the current compilers all come with disassembly, you can have a look at it.

As an enterprise portal and an Office Platform website, php, mysql, and HTML must be used. You can use dreamweaver, wamp, and other environments, I used "elaborate on php" when I first studied it. As for HTML, there are entry-level documents everywhere on the Internet, which are basically used for the interface of the artist. dreamweaver is very simple.

Besides php, java is also a good choice. Although I don't know it, java is still very powerful in website maintenance and so on. I don't know it, but I can only make one proposal.

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.