【发布时间】:2011-12-21 06:52:48
【问题描述】:
我有许多从纯虚基派生的类:
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;}
};
为了简洁起见,我只放了两个派生类,但实际上我还有更多。
我想创建一个类,它有一个指向基的 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;
};
如何获得此功能?
【问题讨论】:
-
您忘记将
base::f()标记为virtual -
我想知道错误信息到底是什么。
标签: c++ boost initialization shared-ptr member