c++ - 类中的 vector 在引用类中为空

标签 c++ vector variable-assignment

我可以将我的问题浓缩为以下问题:

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/

相关文章:

c++ - 为什么我不能在赋值的右侧放置一个指向 const 的指针?

python-3.x - 赋值操作Python

c++ - 你能在 VS 2005 中创建一个 lib 或 dll 并与 VS 2008 链接吗

c++ - 带 openGL glRecti 的棋盘格

opencv - 在 OpenCV 中写入向量

C++ RGB 图像数据叠加到二维数组中( vector 的 vector )

c# - Make efficient - 在 c# 中使用两个向量的对称矩阵乘法

python - python 中 string.partition 的良好实践

c++ - setitimer 信号似乎只在 fork 之后才有效

c++ - 将引用包装器的 vector 转换为基类,将引用包装器的 vector 转换为派生类,不能动态转换