我可以将我的问题浓缩为以下问题:
Class1 x;
Class1 y;
x.Label = "Test";
y = x;
x.myVector.push_back("test");
结果:
x.myVector.size()
== 1,y.myVector.size()
== 0,但两者都有标签“Test”!
我对 C++ 比较陌生,但不幸的是我无法通过在互联网上搜索来解决问题......
感谢您的帮助!
最佳答案
您的示例远未完成,因此我将假设最简单的编译方法:
// creates an instance named x on the stack
Class1 x;
// creates an instance named y on the stack
Class1 y;
// sets the label of the x instance to "Test"
x.Label = "Test";
// COPIES all data from x over to y (including the label)
y = x;
// inserts into the vector of x, as the copy has gone through already, this is in x only
x.myVector.push_back("test");
关于c++ - 类中的 vector 在引用类中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53670678/