我有一个包含大量数据的类,称为HeavyData
. 此类遵循三规则(它重写了复制构造函数、复制赋值运算符和析构函数,以便someBigAmountOfData
在复制类时能够正确复制成员变量并能够释放类而不会导致内存泄漏)。
该类DataManager
有两个类型的成员变量HeavyData
。(见下文)
class HeavyData
{
public:
HeavyData();
HeavyData(const HeavyData& that);
HeavyData& operator=(const HeavyData& that);
~HeavyData();
private:
void* someBigAmountOfData; //maybe a few hundred bytes (on the heap, of course)
size_t sizeOfData;
};
class DataManager
{
public:
DataManager();
//method 1
DataManager(HeavyData one, HeavyData two):
one(one),
two(two)
{
}
//method 2 (which I think is more effective than method 1)
DataManager(const HeavyData& one, const HeavyData& two):
one(one),
two(two)
{
}
private:
HeavyData one;
HeavyData two;
};
问题 :
该类DataManager
有两个构造函数,如下所示:
DataManager(HeavyData one, HeavyData two);
//方法一DataManager(const HeavyData& one, const HeavyData& two);
//方法二
问题在于从上述两个中选择一个构造函数。你觉得哪一个更有效率?为什么?
认为第二个构造函数(方法2)更有效。