I am somehow aware of returning by reference by this got me confused.
I have a function which returns a reference to a public field of class BoardNode
, std::vector<BoardNode> _neighboursVector
.
I also have a class Board
which holds a std::vector<BoardNode>
.
my member function goes like this:
const std::vector<BoardNode>&
Board::getNeighboursVector(unsigned int x, unsigned int y) const
{
BoardNode node = this->getBoardNode(x, y);
//...
node._neighboursVector.push_back(...);
//...
return node._neighboursVector;
}
While debugging on return line I get the correct values in the vector but outside of this function I get empty vector. Why ?
std::vector<BoardNode> v = b.getNeighboursVector(5,5);
EDIT
getBoardNode
definitions
const BoardNode & Board::getBoardNode(unsigned int rowIdx, unsigned int colIdx) const
{
//...
}
BoardNode & Board::getBoardNode(unsigned int rowIdx, unsigned int colIdx)
{
//...
}