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 do C++ pointers call class member functions

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the "C++ pointer how to call class member functions" related knowledge, in the actual case of the operation process, 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!

In programming work will often encounter in a "class" through the function pointer to call member functions in the requirements, for example, when in a class using the C++ standard library of sorting function qsort, as a result of the qsort parameter requires a "compare function" pointer, if the "class" using a member function as a "compare function", you need to pass the pointer of the member function to qsort for its call. The member functions that call "classes" with pointers discussed in this article include the following three situations:

(1). Assign the member function pointer of the "class" to the non-member function pointer of the same type, such as:

Example 1

# include typedef void (* Function1) (); / / defines a function pointer type. Function1 F1; class Test1 {public: / / … The called member function. Void Memberfun1 () {printf ("% s\ n", "Calling Test3::Memberfun2 OK");}; / / void Memberfun2 () {f1=reinterpret_cast (Memberfun1); / / assign member function pointers to F1. An error occurred in the compilation. F1 ();} / /... }; int main () {Test1 T1; t1.Memberfun2 (); return 0;}

(2) within a "class", there are standard library functions, such as qsort, or other global functions, that call member functions of the class with function pointers. Such as:

Example 2:

# include class Test2 {private: int data [2]; / / … Public: / / … Int _ _ cdecl Compare (const void* elem1, const void* elem2) / / member function. {printf ("% s\ n", "Calling Test2::Memberfun OK"); return * ((int*) elem1)-* ((int*) elem2);} void Memberfun () {data [0] = 2; data [1] = 5; qsort (data, 2, sizeof (int), Compare); / / standard library functions are called into / / member functions. An error occurred in the compilation. } / /... }; int main () {Test2 T2; t2.Memberfun (); / / call member functions. Return 0;}

(3) within the same class, one member function calls another member function, such as:

Example 3:

# include "stdlib.h" class Test3 {public: / / … Void Memberfun1 (void (* f2) ()) {f2 ();} / / member function 1 calls member function / / 2. Void Memberfun2 () {printf ("% s\ n", "Calling Test3::Memberfun2 OK");} / / member function 2. Void Memberfun3 () {Memberfun1 (Memberfun2);} / / compilation error / /... }; int main () {Test3 T3; t3.Memberfun3 (); / / call member functions. Return 0;}

There are no significant errors in the syntax of the code in the above three cases, and in some earlier compilation environments, such as VC++ 4.0, it is usually possible to compile, or at most give a problem reminder (Warning). Later compilation tools, such as VC++6.0 and other commonly used C++ compilers, failed to compile the above code and pointed out the following errors (in the third case, using VC++6.0 as an example):

Error C2664: 'Memberfun1': cannot convert parameter 1 from' void (void)'to 'void (_ _ cdecl *) (void)'

None of the functions with this name in scope match the target type

That is, the function called in the Memberfun1 parameter is of the wrong type.

According to the above tips, errors cannot be eliminated simply by changing the type of functions, but if you simply take these functions out of the definition of the class without making any changes, you can eliminate errors by compiling, and still take the third case as an example. the following code can be compiled:

# include void Memberfun1 (void (* f2) ()) {f2 ();} / the original member function 1 calls the member function / / 2. Void Memberfun2 () {printf ("% s\ n", "Calling Test3::Memberfun2 OK");} / / original member function 2. Void Memberfun3 () {Memberfun1 (Memberfun2);} int main () {Memberfun3 (); return 0;}

The first and second cases are exactly the same as the third case.

It can be concluded that the reason why the compilation can not pass in the above three cases does not lie in the wrong function type call, but is related to the "class". In the case of failing compilation, the member function of the class is called with the function pointer, and the non-member function is called with the function pointer, and the type of the function is exactly the same. So, is there any difference between the member function pointer and the non-member function pointer of the class?

In the following program, you can use the sizeof () function to view the length (size) of member and non-member function pointers of various "classes" and output them to the screen.

# include "stdafx.h" # include # include class Test; / / an undefined class. Class Test2 / / an empty class. {}; class Test3 / / A defined class. {public: / /... Void (* memberfun) (); void Memberfun1 (void (* f2) ()) {f2 ();} / / member function 1 calls member function / / 2. Void Memberfun2 (); / / member function 2. / / … }; class Test4: virtual Test3, Test2 / / A class with virtual inheritance (derivative class). {public: void Memberfun1 (void (* f2) ()) {f2 ();}}; class Test5: Test3,Test2 / / an inherited class (derivative class) {public: void Memberfun1 (void (* f2) ()) {f2 ();}}; int main () {std::cout

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