我试图尽可能使用SSCE我的问题,但它涉及多个在C++中定义的对象。他们是简单的,但 - 我认为它最好的,如果我进一步解释之前分享我的代码:修改一个对象与修改该对象的副本
#include <iostream>
#include <vector>
struct Cell {
bool visited;
Cell():visited(false) {}
void setVisited(bool val) {visited = val;}
bool beenVisited() {return visited;}
};
struct Vector2D
{
int size;
std::vector<Cell> myVector;
Vector2D(int n): size(n), myVector(n*n) {}
Cell& getAt(int x, int y) {return myVector[((x * size) +y)];}
};
int main()
{
Vector2D vec = Vector2D(1);
Cell cell= vec.getAt(0,0);
cell.setVisited(true);
cell = vec.getAt(0,0);
if (cell.beenVisited() == false)
std::cout << "Why is this not true like I set it a moment ago?\n";
}
我道歉,真诚为所有这一切,但它是需要做出点。正如你所看到的,我得到了At()我认为是Cell对象,将其访问的实例数据设置为true,然后关闭到另一个单元格。那么,为什么当我回到同一个单元时,发现访问的值是错误的,而不是真的?!这就像它没有注册我的私人数据变化!
这样做的最好方法是什么?
感谢
否。[_this_](http://ideone.com/2fSjR)是[SSCCE](http://sscce.org)。 27行代码,与您的程序有相同的问题。 –