Using C pointers, I can do something like this:
#include <iostream>
#include <memory>
class Foo {
void bar(){
std::cout<<"Hello from Foo::bar \n";
}
}
void main(){
Foo foo;
Foo* foo_ptr=&foo;
std::shrared_ptr<Foo> foo_sptr(&foo);
void (Foo::*bar_ptr)()=&Foo::bar;
(foo.*bar_ptr)();
(foo_ptr->*bar_ptr)();
If I want to use smart_ptr instead of a C pointer, I get a compiler error:
error: no operator "->*" matches these operands
operand types are: std::shared_ptr<Foo> ->* void (Foo::*)()
(foo_sptr->*bar_ptr)();
Is there a way to make this work without std :: shared_ptr :: get ()?
source
share