3
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

class BASE
{
public:
    int fun1(int i){return i * 1;}
};

int main(){
    int (BASE::*pf2)(int);
    boost::shared_ptr<BASE> pB = boost::make_shared<BASE>();
    pf2 = &BASE::fun1;
    std::cout << (pB->*pf2)(3) << std::endl; // compile wrong: error: no match for 'operator->*' in 'pB ->* pf2'|
}

这是否意味着 Boost 库没有实现 '->*' 运算符来支持使用它来调用成员函数指针?

4

2 回答 2

4

你应该写:

std::cout << ((*pB).*pf2)(3) << std::endl;

正如我所检查的,Boost 没有->*为任何指针定义运算符,尽管这是可能的(参见 C++ 标准,第 5.5 和 13.5 节)。

此外,C++11 标准没有为 C++11 智能指针定义此运算符。

于 2012-04-16T11:37:46.580 回答
3

我猜你应该这样做:

std::cout << ((*pB).*pf2)(3) << std::endl;

虽然它没有经过测试。

于 2012-04-16T11:38:54.333 回答