简体   繁体   English

命名空间中的成员函数指针

[英]pointer to member function inside namespace

I've got a namespace MyNamespace containing a class MyClass with many static public members functions. 我有一个名称空间MyNamespace其中包含一个具有许多静态公共成员函数的MyClass类。

What I need to do, is to build, inside the namespace, a map containing a pointer on every public members functions of the class Here the code: 我需要做的是在命名空间内部构建一个包含指针的map ,该指针位于类的每个公共成员函数中。这里代码:

    #include <iostream>
    #include <map>

    namespace MyNamespace {
      class MyClass;

      typedef bool (*fctPtr)(void);
      typedef std::map<std::string, fctPtr> fctMap;
    };

    class MyNamespace::MyClass {

      public:
        static bool func1(void) { return true; };
        static bool func2(void) { return true; };
        static bool func3(void) { return true; };
        static bool func4(void) { return true; };

    };

    MyNamespace::fctMap MyFctMap;


    void execFct() {
      MyNamespace::MyClass obj;
      MyNamespace::fctPtr fctMemb;

      fctMemb = MyFctMap["func1"];
      (obj.*fctMemb)();
    }


    int main() {
      MyFctMap["func1"] = &MyNamespace::MyClass::func1;
      MyFctMap["func2"] = &MyNamespace::MyClass::func2;
      MyFctMap["func3"] = &MyNamespace::MyClass::func3;
      MyFctMap["func4"] = &MyNamespace::MyClass::func4;

      execFct();
    }

And what the compiler says: 而编译器说的是:

    % clang++ draft.cc
    draft.cc:29:7: error: right hand operand to .* has non pointer-to-member type
          'MyNamespace::fctPtr' (aka 'bool (*)()')
      (obj.*fctMemb)();
          ^ ~~~~~~~
    1 error generated.

I don't understand why I got this error neither what to do to resolve the problem. 我不明白为什么我得到这个错误既不能解决问题。 Idea? 理念?

Edit: I'm using c++98 and no boost. 编辑:我正在使用c ++ 98而没有提升。

Working with a typedef bool (MyClass::*fctPtr)(void) drives me to this kind od error, at map assignment time. 使用typedef bool (MyClass::*fctPtr)(void)在地图分配时将我驱动到这种od错误。

 error: assigning to 'mapped_type' (aka 'bool (MyNamespace::MyClass::*)()') from
      incompatible type 'bool (*)()'
  MyFctMap["func1"] = &MyNamespace::MyClass::func1;
                    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Since the functions you are referencing are static, you don't need the obj class reference. 由于您引用的函数是静态的,因此您不需要obj类引用。 Simply call fctMemb(); 只需调用fctMemb(); . You might also consider if you need these functions mapped in such a way, oftentimes you don't need that dynamic aspect to function references in C++ and instead should be using templates. 您也可以考虑是否需要以这种方式映射这些函数,通常您不需要动态方面来在C ++中使用函数引用,而应该使用模板。

A pointer to a function is not the same as a function to a member pointer. 指向函数的指针与成员指针的函数不同。 The important thing to remember is that all (non-static) member function actually have a hidden first argument which is what becomes the this pointer. 要记住的重要一点是,所有(非静态)成员函数实际上都有一个隐藏的第一个参数,它就是this指针。 Therefore a function pointer and a member function pointer can't ever be compatible. 因此,函数指针和成员函数指针不能兼容。

However, you should look into std::function and std::bind : 但是,您应该查看std::functionstd::bind

namespace NyNamespace
{
    typedef std::function<bool()> fctPtr;
    ...
}

MyNamespace::MyClass myObject;
MyFctMap["func1"] = std::bind(&MyNamespace::MyClass::func1, myObject);

The idea is fctMemb(); 这个想法是fctMemb(); :-) :-)
fctPtr, as the compiler says, is not a pointer to member type... 正如编译器所说,fctPtr不是指向成员类型的指针......

Static member functions are not like normal member functions. 静态成员函数与普通成员函数不同。 They have access to class private and protected members, but they are not tied to object instances and thus can't access this pointer nor any non-static members. 他们可以访问类私有成员和受保护成员,但它们不依赖于对象实例,因此无法访问this指针或任何非静态成员。

Their type is that of normal function pointers and they can't be bound to member function pointers (and normal function pointers is what you have in your code). 它们的类型是普通函数指针的类型,它们不能绑定到成员函数指针(正常的函数指针就是代码中的函数)。

You call them like normal functions: 你把它们称为普通函数:

fctMemb();

Btw, the sytax to form member function pointers is different: 顺便说一下,形成成员函数指针的sytax是不同的:

struct Foo {
    void f() {}
};

void (Foo::* mem_fun_ptr)() = &Foo::f;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM