使用复制构造函数C++不能正常工作
C++ with copy constructor doesn't work good
我构建了一个名为 Attribute 的对象,这个对象具有完整的副本和空的构造函数。然后我构建了另一个名为 Human 的对象,其中包含 Attribute 对象。当我尝试构建人类类(使用完整的构造函数)时,它以某种方式自动调用属性复制构造函数,我不知道为什么。
这是代码:
char** d = new char*[3];
d[0] = new char[10];
d[0] = "aa";
d[1] = new char[10];
d[1] = "bb";
d[2] = new char[10];
d[2] = "cc";
Attribute *a = new Attribute(1.7, "blue", "white", "black", d, 3);
Human *h = new Human("Name", *a);
当我使用调试器并到达以下行时:new Human("Name", *a); 它会自动输入此函数:
Attribute::Attribute(Attribute& copy)
{
Attribute(copy.height, copy.eyeColor, copy.skinColor, copy.hairColor, copy.diseases, copy.numOfDiseases);
}
并且只有在这个函数结束后,它才会启动 Human 完整的构造函数......
Human *h = new Human("Name", *a);
^----- Here it's passing in an Attribute by value
因此调用属性复制构造函数。
复制构造函数没有初始化任何东西;它只是创建和销毁一个本地临时对象。
在 C++11 中,您可以将工作委托给另一个构造函数,就像您似乎正在尝试的那样:
Attribute::Attribute(Attribute const & copy) :
Attribute(copy.height, copy.eyeColor, copy.skinColor, copy.hairColor, copy.diseases, copy.numOfDiseases)
{}
从历史上看,您必须复制另一个构造函数的代码,或将其移动到由两个构造函数调用的函数中。
您可能还希望通过引用获取构造函数参数,以便不需要复制它们:
Human(std::string const & name, Attribute const & attribute);
除非你真的需要,否则你也应该避免new
。你可能想要更像的东西
Attribute a(1.7, "blue", "white", "black", d, 3);
Human h("Name", a);
当您确实需要new
时(通常是因为您希望对象超出当前范围,或者有时是为了多态性),请使用 RAII 管理类型(如智能指针和容器)而不是原始指针,以确保在完成对象后正确删除对象。处理原始指针是内存泄漏和其他灾难的秘诀。
a - 是一个指针*a - 是值
因此,如果您的 Human 构造函数按值获取秒数参数
Human::Human(char* s, Attribute a)
它将复制属性并为其使用复制构造函数。如果不希望此行为,可以通过指针传递参数。
Human::Human(char* s, Attribute *a)
并像这样称呼它:
Attribute *a = new Attribute(1.7, "blue", "white", "black", d, 3);
Human *h = new Human("Name", a); // this works with pointer now. Pointer will be copied, but the class will remain in the same memory and wouldn't be copied anywhere.
如果您考虑一下,则行为与正常值和函数类似:
void f1(int a){ a++; cout << a; }
void f2(int *a){ *a++; cout << *a; }
int b = 4;
f1(b); // It copies the value of local b to parameter a, increments local parameter a of f1 and prints it; It will print 5
cout << b; // b will remain the same. It will print 4
f2(&b); // It copies pointer, but both pointers &b and a point to the same variable b from this scope, and it's value is not copied
cout << b; // As we've worked with pointers, it was incremented in f2. It will print 5
请注意,您必须处理所有指针的责任。如果您手动创建了某些内容,请不要忘记应该删除它的位置以及在某些情况下可能存在泄漏。用smart_pointers做到这一点要容易得多。
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程