我試圖儘可能使用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行代碼,與您的程序有相同的問題。 –