说到其他目的,我指的是克隆。这是一个简化的例子:我有对象,有些是正方形,有些是盒子,它们都是Object2D。每个人都有一个唯一的ID。
在这个系统中,我不需要对象拷贝的典型含义。当对象按值传递、按值返回时,将使用典型的复制构造函数,并且...在这个系统中,每个对象都是唯一的,因此我们不需要典型的复制构造函数。两个方 block 至少在 id 上会有所不同。因此,我使用复制构造函数来进行克隆。在克隆过程中,我将深度复制除 id 之外的树结构。
#include <iostream>
#include <string>
#include <memory>
#include <cstdlib>
#include <vector>
int offset(){
return rand()%100-50;
}
int location(){
return rand()%10000-5000;
}
class Object2D{
public:
int x_, y_;
int id;
static int count;
Object2D(int x, int y):x_(x), y_(y){
id=count++;
}
};
int Object2D::count=0;
class Square: public Object2D{
public:
int getParent(){return containedBy_->id;}
Square(Object2D* containedBy, int size):
Object2D(containedBy->x_+offset(), containedBy->y_+offset()),
containedBy_(containedBy),size_(size){
std::cout<<"Square constructor"<<std::endl;
}
Square(const Square& other):
Object2D(other.x_-other.containedBy_->x_, other.y_-other.containedBy_->y_),
containedBy_(other.containedBy_), size_(other.size_){
std::cout<<"Square clone"<<std::endl;
}
private:
Object2D* containedBy_;
size_t size_;
};
class Box:public Object2D{
private:
size_t l_;size_t h_;
std::vector<std::shared_ptr<Square>> all;
public:
void addSquare(int size){
auto temp = std::make_shared<Square>(this, size);
all.push_back(std::move(temp));
}
const std::vector<std::shared_ptr<Square>>& getAll() const{
return all;
}
Box(int x, int y, size_t l, size_t h):
Object2D(x,y), l_(l), h_(h){}
Box(const Box& other):Object2D(location(), location()){// clone the other box, put cloned squares in
for(const auto& item:other.getAll()){
all.push_back(std::make_shared<Square>(*item));
}
}
void showOffsets(){
std::cout<<"show offsets of all components for container"<<id<<":";
for(const auto& item: all){
std::cout<<item->id<<"("<<item->x_-x_<<","<<item->y_-y_<<")"<<" ";
}
std::cout<<std::endl;
}
};
int main()
{
Box b(100,100,100,100);
std::cout<<"before do it"<<std::endl;
b.addSquare(10);
Box c(b);
std::cout<<b.id<<c.id<<std::endl;
b.showOffsets();
c.showOffsets();
return 0;
}
这里我克隆了一个盒子,我还需要克隆包含的方 block ,并保留它们的偏移量。克隆的对象将有新的 id,这些 id 都是唯一的。
问题
使用复制构造函数进行克隆是个好主意吗?请注意,在这种情况下,复制构造函数的行为与典型的复制构造函数不同,因此我需要禁止以任何方式调用通常预期的复制构造函数,例如我不能使用 vector 来包含正方形,因为简单的 vector 删除将调用复制构造函数,它不会保留 id。所以我使用了智能指针。
第二,为什么克隆时偏移量没有被复制?这就是我想保留的东西。
结果:
before do it
Square constructor
Square clone
02
show offsets of all components for container0:1(36,33)
show offsets of all components for container2:3(-1879,2256)
最佳答案
是的,您可以定义自己的复制构造函数。
但是,正如您已经指出的,这样您会发现很难“禁止”您明确不想要的拷贝。
我的建议:给你的对象一个clone()
函数并删除
复制构造函数(也许还有复制运算符)。
这样,您只有在明确想要克隆时才进行克隆。
关于c++ - 可以将 C++ 复制构造函数用于其他目的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728043/