代码之家  ›  专栏  ›  技术社区  ›  GRASBOCK

这个指向非静态成员函数代码的指针不起作用的原因是什么?[副本]

  •  0
  • GRASBOCK  · 技术社区  · 7 年前

    class A {
    public:
        void(*fPtr)();
    };
    
    class B {
        int ib = 2;
    public:
        void memf() {
            printf("ib = %i\n", ib);
        }
    };
    
    class C {
        int ic = 6;
    public:
        void memf() {
            printf("ic = %i\n", ic);
        }
    };
    
    int main()
    {
        B b;
        C c;
        A a1;
        A a2;
        a1.fPtr = b.memf;
        a2.fPtr = c.memf;
    }
    

    基本上是一个具有函数指针的类。 此函数指针可以指向普通函数或成员函数。

    Error   C2440   '=': cannot convert from 'void (__thiscall B::* )(void)' to 'void (__cdecl *)(void)'
    Error   C2440   '=': cannot convert from 'void (__thiscall C::* )(void)' to 'void (__cdecl *)(void)'    
    Error   C3867   'B::memf': non-standard syntax; use '&' to create a pointer to member
    Error   C3867   'C::memf': non-standard syntax; use '&' to create a pointer to member
    

    我知道这看起来像是复制品,也许是。我没有使用推荐的解决方案,比如 <functional> .

    有人能告诉我怎么做才能让我明白我做错了什么吗? 我也想知道为什么标准方法不起作用。

    我会非常感激的。

    2 回复  |  直到 7 年前
        1
  •  2
  •   templatetypedef    7 年前

    B::memf fptr . 该函数需要相对于 B B 物体 ib 领域但我们没办法弄清楚是什么 要使用的对象-糟糕的时刻!

    这个网站上的其他答案应该能够提供一些关于解决这个问题的可能策略的信息,但希望这能让我们了解这个问题首先是什么。

        2
  •  1
  •   Francis Cugler    7 年前

    这里有一个可能解决你问题的方法:不过我做了一些小小的改变。

    • 圣 -我变了 Class A 到 Class Template .
    • 钕 -我添加了一个指向类型的指针 <T>
    • 3. 研发部 -我加了一个 operator()() 到 A<T> 调用函数调用。
    • 4. th -为了保持一致,我更改了函数指针的名称和其他类函数的名称。
    • th -而不是使用 printf() ,我曾经 std::cout .

    以下是修改后的代码的外观: 阅读评论以了解其工作原理。

    template<class T>
    class A {
    private:
        // Need to save a pointer to the class to have a realitive object.
        T* ptrT{ nullptr };
    public:
        // Assigns the pointer of the object to this's class's pointer member.
        // Must pass by reference otherwise you will have undefined behavior.
        explicit A( T& t ) : ptrT( &t ) {}
    
        // Notice the Template Arguments class resolution operator...
        void(T::*func)();
    
        // The added operator()() to invoke the function call.
        // This could of been any arbitrary member function; 
        // I just chose to use the operator() for demonstration.
        void operator()() {
            (ptrT->*func)();
        }
    };
    
    // In class's A & B the only changes here are 
    // the names of the function and replacing printf() with cout
    class B {
        int ib{ 2 };
    public:
        void func() {
            std::cout << ib << '\n';
        }
    };
    
    class C {
        int ic{ 6 };
    public:
        void func() {
            std::cout << ic << '\n';
        }
    };
    
    int main() {
        try {
            B b;
            C c;
            // Changes to A by using templates and passing the object      
            // above to A's explicit constructor
            A<B> a1( b );
            A<C> a2( c );
    
            // Notice the syntax on how to assign the function pointer.
            a1.func = &B::func;
            a2.func = &C::func;
    
            // using the operator() to invoke the function pointer.
            a1();
            a2();
        } catch( std::runtime_error& e ) {
            std::cerr << e.what() << std::endl;
            return EXIT_FAILURE;
        }
        return EXIT_SUCCESS;    
    }
    

    -输出-

    2
    6
    
    推荐文章