小编Mar*_*sar的帖子

创建const share_ptr <pure_virtual_class>成员

我有许多类来自纯粹的基础:

class base {
public:
    virtual int f() = 0;
};

class derived_0 : public base {
public:
    int f() {return 0;}
};

class derived_1 : public base {
public:
    int f() {return 1;}
};
Run Code Online (Sandbox Code Playgroud)

为了简洁,我只放了两个派生类,但实际上我有更多.

我想创建一个具有const共享指针的类.我想做以下但我不能,因为我必须初始化初始化列表中的const指针:

class C{
public:
    C(bool type) { 
        if(type) {
            derived_0* xx = new derived_0;
            x = shared_ptr<base>( xx );
        }
        else {
            derived_1* xx = new derived1;
            x = shared_ptr<base>( xx );
        }
    } 
private:
    const share_ptr<base> x;
};
Run Code Online (Sandbox Code Playgroud)

我该如何获得此功能?

c++ boost initialization member shared-ptr

2
推荐指数
1
解决办法
103
查看次数

标签 统计

boost ×1

c++ ×1

initialization ×1

member ×1

shared-ptr ×1