In C++ how do I get to use a shared pointer of a pure abstract class without using reset and new?
The example is a little contrived but illustrates the problem I am encountering.
Look at the run()
method: reset
works but the commented out lines don't...
#include <iostream>
#include <map>
#include <memory>
using namespace std;
class Interf {
public:
virtual void doSomething()=0;
};
class Foo : public Interf {
public:
Foo() { cout << "Foo constructed\n"; }
shared_ptr<Interf> innerInterf;
void doSomething() {
cout << "Foo:doSomething()\n";
innerInterf->doSomething();
}
void run() {
cout << "run() called\n";
innerInterf.reset(new Bar()); // this works
//Bar b;
//innerInterf = make_shared<Interf>((Interf)b); // how can i get this to work?
}
class Bar : public Interf {
public:
Bar() { cout << "Bar constructed\n"; }
~Bar(){ cout << "Bar destroyed\n"; }
void doSomething() { cout << "Bar:doSomething()\n"; }
};
};
int main() {
Foo foo;
foo.run();
Interf *interf;
interf = &foo;
cout << "interf.doSomething()\n";
interf->doSomething();
}