V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jjyygay
V2EX  ›  问与答

c++中,关于类中成员方法指针的问题,新手求解

  •  
  •   jjyygay · 2017-03-09 10:35:41 +08:00 · 968 次点击
    这是一个创建于 2610 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前看别人源码,发现 std::bind 中关于类成员方法指针的用法: &MyClass::MyFunc; 猜想&是取 MyFunc 函数的地址,于是想加个括号验证下优先级: &(MyClass::MyFunc);却发现编译不过去。所以想请熟悉这方面的热心网友能帮忙解答下,感谢~

    测试代码如下:

    class MyClass { public: void MyFunc() { ; } };

    int main() { &MyClass::MyFunc; /* 编译通过 / //&(MyClass::MyFunc); / 编译失败 */ }

    xss
        1
    xss  
       2017-03-09 11:13:40 +08:00   ❤️ 1
    1. std::bind 函数声明里面的&不是取地址, 而是代表引用.

    2. &(MyClass::MyFunc)失败是因为 MyFunc 是非静态成员, 编译阶段此函数还没有被分配地址.
    jjyygay
        2
    jjyygay  
    OP
       2017-03-09 18:47:38 +08:00
    感谢回答~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2496 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:05 · PVG 22:05 · LAX 07:05 · JFK 10:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.