我有2個成員函數從哪一個是常量返回一個const一個問題:爲什麼我總是使用默認參數獲取對象?
const BoardNode & Board::getBoardNode(unsigned int rowIdx, unsigned int colIdx) const
{
return _mData[rowIdx*_mNumColumns + colIdx];
}
BoardNode & Board::getBoardNode(unsigned int rowIdx, unsigned int colIdx)
{
return _mData[rowIdx*_mNumColumns + colIdx];
}
過了一會兒,我使用的代碼:
// where this is a Board holding Nodes in std::vector
BoardNode nodeToAddAsNeighbor = this->getBoardNode(x1+ x, y1+ y);
無論的y1
值,y
,x
,x1
是,我總是用coords (0,0)
返回節點。
然而,節點的其他參數是不同的,只有座標如上所述。
任何想法爲什麼?
編輯 我的拷貝構造器:
BoardNode::BoardNode(const BoardNode & other) :
_mNodeType(other._mNodeType),
_coordinates(other._coordinates),
_neighboursVector(other._neighboursVector) {}
儘量縮小這個問題了。 –
你的副本構造函數只是做了編譯器提供的人做的事情,不要明確地定義它。 – GManNickG
您確定'_mData'中的元素設置正確嗎?你如何構建它們? – molbdnilo