我试着做以下几件事:
class A {
virtual ~A();
virtual void reset() {
this->~A();
construct(); // this should magically use placement-new of the derived type on 'this', so that if called from an A pointer to a B class, it works.
}
};
class B : public A {
...
};如何以最通用的方式实现构造?
我想使用现有的默认构造函数,这是我要添加到现有的继承结构中的一个更改。我现正考虑两个方案:
构造()是在每个派生类中实现的,它是对新布局的调用。
2)将所有构造函数移动到每个类中的init()函数。这违背了我们的工作方式,也可能会导致一些事情的混乱,而这些事情只能通过初始化程序列表来初始化。
发布于 2014-07-15 16:59:35
“如何以最通用的方式实现构造?”
struct AA {
virtual void reset() = 0;
virtual ~AA() {}
};
template<class Derived>
class A : public virtual AA {
// ^^^^^^^ prevent nasty problems introduced with eventually
// multiple occurrence of AA in the inheritance hierarchy
public:
virtual ~A();
// No construct() necessary
virtual void reset() {
// Have an appropriate default constructor and assignment operator,
// the latter is the equivalent for cloning
static_cast<Derived&>(*this) = Derived();
}
};
class B : public A<B> {
// ...
};用法
B b;
AA* vp = &b;
vp->reset();还有两点:
this->~A();,这会带来麻烦new(),它不仅不适用于您的情况,而且很少做人们通常想做的事情。发布于 2014-07-15 18:07:04
我的两分钱(虽然圆周率的答案更直接地回答你的问题),一个班应该做一件事和一件事单独。通常,您应该将内存管理和类实现分开。你要做的就是压缩一个电话。
获得相同功能的最直接方法是只使用一个血腥的唯一指针(C++11)。
std::unique_ptr<A> a(new B());
a.reset(new B());//deletes the old B and makes a new B.如果您可以使用非连续内存分配(并且支持提示,您几乎总是可以),这是可以的。但让我们说你做不到(因为这违背了你的宗教信仰)。好吧,这几乎和你想要做的一样。
B a;//old reference
a = B();//Reset!这与您编写的“就位”重置调用完全一样,没有任何不良的副作用。
所有这些方法都有一个共同的缺陷。您必须放弃旧引用的资源,以承担新引用的资源。那里没有重复使用,这是一个遗憾。唯一的解决办法是提供一个手动的“重置”功能。STL中的类可以使用"assign“函数来实现这一点,并且通常支持的不仅仅是默认的构造函数。
std::vector<int> x(100);//made a vector x of 100
x.assign(50);//acts like x=std::vector<int>(50), but will most likely reuse the memory这就是PI在CRTP响应中得到的结果:它是否是静态继承,也就是说,它执行一个重置功能而不受多态性的惩罚。
https://stackoverflow.com/questions/24763889
复制相似问题