Does smart pointer support calling a member function using a pointer?

#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'|
}

Does this mean that the Boost library does not implement the '-> *' operator to support the use of this method to call a pointer to a member function?

+3
source share
2 answers

You must write:

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

As I noticed, Boost does not define an operator ->*for any of the pointers, although this is possible (see the C ++ standard, sections 5.5 and 13.5).

In addition, the C ++ 11 standard does not define this operator for C ++ 11 smart pointers.

+4
source

I would suggest that you should do it like this:

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

although it is not tested.

+3
source

All Articles