我在下面的示例中遇到了困难,最后一行产生了“已调用中止”错误。我不明白为什么会这样。
在这种情况下,为了清楚起见,我使用 (*abc).def 而不是 abc->def。
#include <iostream>
#include <string>
#include <vector>
class branch
{
public:
unsigned short n;
std::vector<branch> branches;
branch left()
{
return branches.at(0);
}
};
void main()
{
branch trunk = branch();
trunk.n = 0;
branch b1, b2;
b1.n = 0;
b2.n = 5;
b1.branches.push_back(b2);
trunk.branches.push_back(b1);
branch* focus1 = &(trunk.branches.at(0));
branch* focus3 = &(trunk.left());
std::cout<<trunk.left().branches.at(0).n<<std::endl; // ok
std::cout<<(*focus1).branches.at(0).n<<std::endl; // ok
std::cout<<(*focus1).left().n<<std::endl; // ok
std::cout<<(*focus3).branches.at(0).n<<std::endl; // problem
}