我不知道,为什么在从重载运算符返回const引用时得到SIGSEGV,但是当我返回值时没有问题。此外,我已在http://cpp.sh/检查了我的代码,并且执行时没有错误。我使用Qt Creator 4.2.1和MinGW 5.3(但也直接从命令行编译,结果相同)。这是我的代码:
using TCardsTruthMatrix = std::vector<std::vector<bool>>;
struct CardsTruthMatrix_S
{
CardsTruthMatrix_S(TCardsTruthMatrix matrix) : m_matrix(matrix) {}
TCardsTruthMatrix m_matrix;
const bool& operator()(unsigned x, unsigned y) //const bool operator-ok
{
return m_matrix.at(x).at(y);
}
};
我是如何获得价值的:
TCardsTruthMatrix handWithBoard;
for(int i = 0 ; i < 13; i++)
{
std::vector<bool> vec(4, false);
handWithBoard.push_back(vec);
}
CardsTruthMatrix_S board = handWithBoard;
if(!board(1,1)) // SIGSEGV here
{
cout << "Zzzz" << endl;
}