Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply C++ pointer

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to apply C++ pointers". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Declaration of member function pointers

A member function pointer includes the return type of the member function, followed by:: operator class name, pointer name, and function parameters. At first glance, the grammar is a little complicated. In fact, it can be understood as a pointer to the original function, the format is: function return type, class name,:: operator, pointer asterisk, pointer name, function argument.

A pointer to an external function is declared as:

Void (* pf) (char *, const char *); void strcpy (char * dest, const char * source); pf=strcpy

A pointer to a class A member function is declared as:

Void (A::*pmf) (char *, const char *)

The interpretation of the declaration is that pmf is a pointer to a member A function that returns an untyped value, and the function takes two arguments of type char * and const char * respectively. Except for the addition of AVOR: before the asterisk, the same way you declare external function pointers.

Assignment

The way to assign a value to a member pointer is to assign the function name through the pointer symbol & to the pointer name. As follows:

Class A {public: void strcpy (char *, const char *); void strcat (char *, const char *);}; pmf = & A::strcpy

Some older compilers can assign values without an & sign, but the standard C++ requires a & sign to be added.

Use type definition

You can use type definitions to hide complex member pointer syntax. For example, the following statement defines that PMA is a pointer to a member A function that returns untyped values, and the function argument types are char * and const char *:

Typedef void (A::*PMA) (char *, const char *); PMA pmf= & pmf is a variable of type PMF (class A member pointer)

As you'll see below, the use of type definitions is particularly useful for declaring an array of member pointers.

Call a member function through a member pointer

You can call the member function of an object through a member pointer without knowing the name of the function. For example, the function dispatcher has a variable pmf that calls the class member function, regardless of whether it calls the strcpy () function or the strcat () function. There is a big difference between pointers to external primitive functions and pointers to class member functions. The latter must point to the host object of the tuned function. Therefore, in addition to having member pointers, there must be legal objects or object pointers.

Now an example is given to further illustrate. Suppose A has two instances, and the member function pointer supports polymorphism. This is handled dynamically when the member pointer calls the virtual member function. Note that constructors and destructors are not callable. Examples are as follows:

An A1, a2; A * p = & A1; / / create a pointer to A / / create a pointer to a member and initialize void (A::*pmf) (char *, const char *) = & A char char strcpy; / / to bind a member function to a pmf, you must define the object to call. / / you can use the * sign to guide: void dispatcher (An a, void (A::*pmf) (char *, const char *)) {char str [4]; (a.*pmf) (str, "abc"); / / bind the member function to pmf} / / or point to the member pointer in the pointer expression of A: void dispatcher (A * p, void (A::*pmf) (char *, const char *)) {char str [4] (p-> * pmf) (str, "abc");} / / the function can be called by: dispatcher (a, pmf); /. * dispatcher (& a, pmf); / /-> * mode

Advanced use skills

The above is the basic knowledge of member functions. Now introduce its advanced skills.

Member pointer array

In the following example, an array containing two member pointers is declared and the member function address of the class is assigned to the member pointer:

PMA pmf [2] = {& A::strcpy, & A::strcat}

Such arrays are useful in menu-driven applications. After selecting the menu item, the application will call the corresponding callback function, as shown below:

Enum MENU_OPTIONS {COPY, CONCAT}; int main () {MENU_OPTIONS option; char str [4]; / / read options from external resources switch (option) {case COPY: (pa- > * PMF [copy]) (str, "abc"); break; case CONCAT: (pa- > * PMF [confit]) (str, "abc"); break; / /... }}

Member functions of type Const

The type of the member pointer should be the same as the member function type. The pmf in the above example can point to any function of A, as long as the function is not of type const. As shown below, assigning the address of touppercase () to pmf will result in a compilation error because the type of touppercase () is const.

Class A {public: void strpcy (char*, const char*); void strcat (char*, const char*); void touppercase (char*, const char*) const;}; pmf=&A::touppercase; / / error, type mismatch / / the solution is to declare a member pointer of const type: void (A::pcmf) (char*, const char*) const; pcmf=&A::touppercase; / / now

Some poor compilers allow a non-const member pointer to a const member function. This is not allowed in C++.

This is the end of the content of "how to apply C++ pointers". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report