Home > Backend Development > C++ > C++ syntax error: member function pointer cannot point to non-member function, how to deal with it?

C++ syntax error: member function pointer cannot point to non-member function, how to deal with it?

王林
Release: 2023-08-22 16:43:47
Original
1519 people have browsed it

C++ syntax error: member function pointer cannot point to non-member function, how to deal with it?

In C programming, a member function pointer is a pointer to a member function of a class. Using member function pointers allows you to dynamically select which member function to call at run time, which is a very useful technique. However, sometimes we encounter a problem, that is, member function pointers cannot point to non-member functions. How should we deal with this?

First of all, you need to understand why member function pointers cannot point to non-member functions. This is because the types of non-member functions are different from those of member functions. Member functions need to implicitly pass this pointer in order to access member variables and functions of the class. Therefore, the member function pointer saves the address of the member function and the class pointer, while the non-member function does not have this pointer and cannot access the member variables and functions of the class, so the member function pointer cannot point to a non-member function.

We can solve this problem through some techniques. Here are some solutions:

  1. Use functors or Lambda expressions

A functor is a class that implements a function call operator. We can overload the function call operator so that objects of a class can be called like functions. Therefore, we can use a functor to implement the function of a member function, so that the member function pointer can point to the functor object. Lambda expressions can also achieve the same functionality.

The following is an example of using a functor:

#include <iostream>

class MyClass {
public:
    void foo() {
        std::cout << "Hello from foo!";
    }
};

class FunctionObject {
public:
    void operator()() {
        obj.foo();
    }
    MyClass obj;
};

int main() {
    FunctionObject f;
    f();
    return 0;
}
Copy after login

In the above code, we define a FunctionObject class, which contains a MyClass object and an operator() function. When the functor When the child object is called, the operator() function is called, thereby calling the foo function of MyClass.

We can call the foo function by passing the address of the FunctionObject object to the member function pointer. For example:

void (MyClass::*func_ptr)() = &MyClass::foo;
FunctionObject f;
MyClass* p = &(f.obj);
(p->*func_ptr)();
Copy after login

In the above code, we define a member function pointer func_ptr, pointing to the foo function of MyClass. Then, we created a FunctionObject object f and assigned the address of its obj member to the MyClass pointer p. Finally, we use the member access operator (p->*) and the member function pointer func_ptr to call the foo function.

  1. Use global functions and void* pointers

Another solution is to use global functions and void pointers. We can define a global function that accepts a void pointer as a parameter, converts it to a pointer to the class, and calls the member function. Then, we can assign the address of this global function to the member function pointer to call the member function of the class.

The following is an example of using a global function and a void* pointer:

#include <iostream>

class MyClass {
public:
    void foo() {
        std::cout << "Hello from foo!";
    }
};

void invoke_function(void* obj_ptr, void (*func_ptr)()) {
    MyClass* p = static_cast<MyClass*>(obj_ptr);
    (p->*func_ptr)();
}

int main() {
    void (*func_ptr)() = &MyClass::foo;
    MyClass obj;
    invoke_function(&obj, func_ptr);
    return 0;
}
Copy after login

In the above code, we define a global function invoke_function, which accepts a void pointer and a function pointer as parameters. In the invoke_function function, we convert the void pointer obj_ptr to the MyClass pointer p, and use the member access operator (p->*) and the function pointer func_ptr to call the foo function. In the main function, we define a member function pointer func_ptr, pointing to the foo function of MyClass. Then, we create a MyClass object obj and pass its address to the invoke_function function, and then pass func_ptr to the invoke_function function.

Summary

Member function pointer is a powerful tool that allows us to dynamically select the member function to be called at runtime. However, when using member function pointers, if we need to point to non-member functions, we can use functors or Lambda expressions, or use global functions and void* pointers. These techniques can help us solve the problem that member function pointers cannot point to non-member functions.

The above is the detailed content of C++ syntax error: member function pointer cannot point to non-member function, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template