【问题标题】:Is it ok to use C++ copy constructor for other purpose?可以将 C++ 复制构造函数用于其他目的吗?
【发布时间】:2018-06-06 19:19:46
【问题描述】:

我的意思是克隆。这是一个简化的示例:我有对象,有些是正方形,有些是盒子,它们都是 Object2D。每个人都有一个唯一的 id。

在这个系统中,我不需要对象副本的典型含义。当对象按值传递,按值返回时使用典型的复制构造函数,并且......在这个系统中,每个对象都是唯一的,所以我们不需要典型的复制构造函数。至少 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;
}

这里我克隆了一个盒子,我还需要克隆包含的方块,并保持它们的偏移量。克隆的对象将具有新的 id,它们都是唯一的。

问题

使用复制构造函数进行克隆是个好主意吗?请注意,在这种情况下,复制构造函数的行为将不同于典型的复制构造函数,因此我需要禁止以任何方式调用通常预期的复制构造函数,例如我不能使用向量来包含正方形,因为一个简单的向量擦除将调用不会保留 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) 

【问题讨论】:

  • 我不明白你在做什么。是不是当你复制一个实例时,你想将该实例添加到拥有原始实例的容器中?通过阅读您的代码,我没有看到您的复制构造函数有什么特别之处。
  • “当对象按值传递,按值返回时使用典型的复制构造函数”你错过了一个,当你想复制一个对象时......这就是你在这里所做的。
  • @FrançoisAndrieux,我正在尝试将复制的实例放入新容器中,但不知道如何操作。我只能从其他人那里得到原始容器。

标签: c++ copy-constructor deep-copy


【解决方案1】:

是的,您可以定义自己的复制构造函数。

但是,正如您已经指出的那样,这样您会发现很难在您明确不想要的地方“禁止”复制。

我的建议:给你的对象一个clone() 函数和delete 复制构造函数(也可以是复制操作符)。

这样你只有在你明确打算克隆时才克隆。

【讨论】:

    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多