小编Los*_*Lad的帖子

如何在不使用reset和new的情况下使用纯抽象类的共享指针?

在 C++ 中,如何在不使用 reset 和 new 的情况下使用纯抽象类的共享指针?

这个例子有点做作,但说明了我遇到的问题。

看看run()方法:reset有效,但注释掉的行不......

#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?
    } …
Run Code Online (Sandbox Code Playgroud)

c++

19
推荐指数
1
解决办法
1103
查看次数

标签 统计

c++ ×1