复制具有const向量成员的类的构造函数

时间:2018-11-08 14:34:35

标签: c++ const copy-constructor

我的代码中有OpenTable类:

class OpenTable : public BaseAction {
public:
    OpenTable(int id, std::vector<Customer *> &customersList);
    OpenTable(const OpenTable& other);
private:
    const int tableId;
    const std::vector<Customer *> customers;
};

我需要为该类实现一个复制构造函数(我知道这可能是一个糟糕的设计,但我被指示这样做)。尝试深度复制const向量customers时遇到问题。

我尝试了以下操作:

OpenTable::OpenTable(const OpenTable& other): tableId(other.tableId)
{
    for(std::vector<Customer*>::const_iterator i=other.customers.begin(); i!=other.customers.end(); i++) {
         customers.push_back((*i)->copy()); // copy returns a Customer*
    }

但是显然它不能编译,可能是因为向量是const,所以我不能在其中添加元素。

我收到以下错误:

no instance of overloaded function "std::vector<_Tp, _Alloc>::push_back 
[with _Tp=Customer *, _Alloc=std::allocator<Customer *>]" matches the 
argument list and object (the object has type qualifiers that prevent a 
match) -- argument types are: (Customer *) -- object type is: const 
std::vector<Customer *, std::allocator<Customer *>>

注意:在参数化的构造函数中,我只是浅抄,因为可以这样做。但是,这不适用于复制构造函数。

谢谢。

3 个答案:

答案 0 :(得分:2)

最简单的解决方案是创建一个辅助函数,该函数接受一个const std::vector<Customer *>并返回一个std::vector<Customer *>,它是参数的深层副本。然后,在构造函数的初始化程序列表中,使用该函数初始化您的私有成员。

(可选)如果您想限制对它的访问,该辅助函数可以是您班上的privatestatic成员。这在构造函数或其初始化程序列表中是可以接受的,因为static成员函数不依赖于初始化的类的非静态成员,因此也不依赖于完成的构造函数。

如果在进行深层复制的辅助函数中出现问题,则需要适当清理辅助函数(例如,如果Customer对象之一的构建失败,则必须释放任何成功构建的对象以避免内存泄漏)。

答案 1 :(得分:1)

作为成员初始化程序的一部分,您将需要类似boost::transform_iterator的操作来进行复制。

auto copy_iter(auto iter)
{
    return boost::make_transform_iterator(iter, [](Customer * c){ return c->copy(); });
}

OpenTable::OpenTable(const OpenTable& other)
 : tableId(other.tableId),
   customers(copy_iter(other.customers.begin()), copy_iter(other.customers.end()))
{}

OpenTable::OpenTable(const OpenTable& other)
 : tableId(other.tableId),
   customers(boost::copy_range<std::vector<Customer *>>(other.customers | boost::adaptors::transformed([](Customer * c){ return c->copy(); })))
{}

答案 2 :(得分:0)

为什么要使用const类成员?你与他们无能为力。将公共访问功能改为只读。

std::vector<Customers *> const getCustumers() const { return customers;}
int const getTableId() const { return tableId; }

这将为您提供只读的客户向量或无法修改的表ID。比复制构造函数没问题。

相关问题