问题描述
我正在试验 CRTP 的概念以及如何使用它来近似 C++ 中的 mixin。
我开发了以下代码来说明这个想法,但是当矢量 shapeVec 尝试删除智能指针时遇到了一个问题,它会导致分段错误。
有人可以解释这种方法有什么问题吗? 谢谢。
#include <memory>
#include <vector>
#include <iostream>
using namespace std;
struct Shape
{
virtual unique_ptr<Shape> clone ()= 0;
virtual int getX() = 0;
virtual ~Shape()=default;
};
template <typename T>
struct CloneMixin
{
unique_ptr<Shape> clone ()
{
return unique_ptr<T>(static_cast<T*>(this));
}
};
template <typename T>
struct GetterMixin
{
int getX()
{
return static_cast<T*>(this)->x;
}
};
struct Square : public CloneMixin<Square>,public GetterMixin<Square>,public Shape
{
int x=1;
virtual unique_ptr<Shape> clone() override { return CloneMixin::clone();}
virtual int getX() override { return GetterMixin::getX();}
virtual ~Square()=default;
};
struct Rectangle : public CloneMixin<Rectangle>,public GetterMixin<Rectangle>,public Shape
{
int x=2;
int y=3;
virtual unique_ptr<Shape> clone() override { return CloneMixin::clone();}
virtual int getX() override { return GetterMixin::getX();}
virtual ~Rectangle()=default;
};
int main()
{
vector < unique_ptr<Shape>> shapeVec;
shapeVec.push_back(make_unique< Square>());
shapeVec.push_back(make_unique<Rectangle>());
for (auto &i : shapeVec)
{
unique_ptr<Shape> ss = i->clone();
cout << ss->getX()<<endl;
}
return 0;
}
编辑:我的克隆功能有误。它返回 shapeVec 中对象的相同指针。在删除主函数中的 (ss) 唯一指针时,此类对象被删除一次。当 shapeVec 尝试删除自己的指针时,它们之前已经删除了。这导致了异常。
我将 CloneMixin 中的 clone 函数更改为以下内容,它按预期工作:
unique_ptr<T> clone ()
{
return make_unique<T>(*static_cast<T*>(this));
}
解决方法
问题在于您实际上并没有在 CloneMixin::clone
中进行克隆。您正在返回一个新的 unique_ptr,它管理与您应该克隆的对象相同的对象。这显然是一个错误。解决方案是实际制作副本。
假设每个形状都有一个复制构造函数,那么你可以做类似的事情
template <typename T>
struct CloneMixin
{
unique_ptr<Shape> clone()
{
T* this_shape = static_cast<T*>(this);
return unique_ptr<T>( new T(*this_shape) );
}
};
你也可以在那里使用 make_unique<T>
,并且成员函数可以是 const
,给定合适的 const ref 复制构造函数。