我正在创建一个名为 Team 的类。如何让两个对象作为合作伙伴相互指向?这是我迄今为止一直在做的事情。我仅使用布尔数据字段对其进行了测试,并且我知道我的程序的其余部分到目前为止都可以正常工作。
#include <iostream>
using namespace std;
class Team
{
public:
Team();
Team(bool prac, Team *part);
Team *partner;
bool practiced;
void createPairing (Team *t1, Team *t2);
};
Team::Team()
{
practiced = false;
partner = 0;
}
Team::Team(bool prac, Team *part)
{
practiced = prac;
partner = part;
}
void Team::createPairing (Team *t1, Team *t2)
{
t1->partner = t2;
t2->partner = t1;
}
int main()
{
Team Harry;
Team Ron;
Team *p1 = &Harry;
Team *p2 = &Ron;
Team createPairing(p1, p2);
cout << Harry.partner << endl;
return 0;
}