You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何从main.cpp访问类中指向函数的指针动态数组?

Hey there! Let's sort out this function pointer array issue you're facing. The main gotcha here is that non-static class member functions have an implicit this pointer, which means they can't be stored in a regular function pointer array directly. Let's walk through two solutions depending on whether your functions need to access non-static class members.


Solution 1: Use Static Member Functions (No this Required)

If your f1, f2, f3 don't need to access any non-static member variables or functions of class A, this is the simplest approach. Static member functions don't have an implicit this pointer, so they're compatible with regular function pointers.

A.h

#ifndef A_H
#define A_H

#include <iostream>

// Define a typedef for our regular function pointer type
typedef void (*FuncPtr)();

class A {
public:
    // Declare static member function prototypes
    static void f1();
    static void f2();
    static void f3();

    // Public function pointer array (or use a getter if you prefer encapsulation)
    static FuncPtr funcArray[];
};

#endif

A.cpp

#include "A.h"

// Implement the static member functions
void A::f1() {
    std::cout << "This is f1: " << std::endl;
}

void A::f2() {
    std::cout << "This is f2: " << std::endl;
}

void A::f3() {
    std::cout << "This is f3: " << std::endl;
}

// Initialize the function pointer array with our static functions
FuncPtr A::funcArray[] = {A::f1, A::f2, A::f3};

main.cpp

#include "A.h"

int main() {
    // Access the static array directly and call f1
    A::funcArray[0]();
    return 0;
}

Solution 2: Use Member Function Pointers (For Non-Static Members)

If your functions need to interact with non-static members of A, you'll need to use member function pointers instead. These pointers account for the implicit this pointer required to call non-static members.

A.h

#ifndef A_H
#define A_H

#include <iostream>

class A {
public:
    // Declare non-static member function prototypes
    void f1();
    void f2();
    void f3();

    // Define a typedef for our member function pointer type
    // Syntax: return_type (Class::*)(parameter_list)
    typedef void (A::*MemberFuncPtr)();

    // Public member function pointer array
    static MemberFuncPtr funcArray[];
};

#endif

A.cpp

#include "A.h"

// Implement the non-static member functions
void A::f1() {
    std::cout << "This is f1: " << std::endl;
}

void A::f2() {
    std::cout << "This is f2: " << std::endl;
}

void A::f3() {
    std::cout << "This is f3: " << std::endl;
}

// Initialize the member function pointer array
// Note: We use &A::f1 to get the address of the member function
A::MemberFuncPtr A::funcArray[] = {&A::f1, &A::f2, &A::f3};

main.cpp

#include "A.h"

int main() {
    // Non-static member functions require an instance of the class
    A myAInstance;

    // Call the member function pointer: (instance.*pointer)()
    // The parentheses are crucial to avoid operator precedence issues
    (myAInstance.*A::funcArray[0])();

    return 0;
}

Key Notes to Remember

  • For static functions: Use regular function pointers, no instance needed to call.
  • For non-static functions: Use member function pointers, and always call them through a class instance (either with .* for a stack object or ->* for a pointer to an object).
  • When taking the address of a member function, always use &ClassName::FunctionName (even though some compilers let you omit the &, it's standard practice to include it).

内容的提问来源于stack exchange,提问作者Kevag6

火山引擎 最新活动