1
这是list类中节点的构造函数。我需要在初始化列表中创建一个酒庄的深层副本,另一个类。项目是酿酒厂的一个实例。如何在构造函数初始化列表中进行深层复制。 C++
List::Node::Node(const Winery& winery) :
item(winery)
// your initialization list here
{
Winery w = winery;
item = w;
}
构造函数酒庄:
Winery::Winery(const char * const name, const char * const location,
const int acres, const int rating) :
name(name),
location(location),
acres(acres),
rating(rating)
{
char *nm = new char[strlen(name) + 1];
char *loc = new char[strlen(location) + 1];
strcpy(nm, this->name);
strcpy(loc, this->location);
this->name = nm;
this->location = loc;
this->acres = acres;
this->rating = rating;
}
为'winery'制作一个ctor,它可以进行深度复制。顺便说一句:ctor-init-list和ctor-body看起来像重复其他工作。 – Deduplicator 2014-10-11 03:50:01
你说得对,我会把它说出来。我添加了我对酿酒厂构造函数的代码。 – 2014-10-11 03:55:55
“深层复制”与“浅层复制”通常只用于仅支持GC的语言,而不直接支持值(而不仅仅是指针)。 – o11c 2014-10-11 04:06:47