#include <stdio.h>
struct Person {
char *Name;
int Age;
int Sex;
struct Person *Couple;
};
struct Person P1;
struct Person P2;
int main(void)
{
printf("Hello World!\r\n");
P1.Name = "P1";
P1.Age = 30;
P1.Sex = 1;
P1.Couple = &P2;
P2.Name = "P2";
P2.Age = 30;
P2.Sex = 0;
P2.Couple = &P1;
printf("P1's couple name is %s\r\n", P1.Couple->Name);
printf("P1's couple age is %d\r\n", P1.Couple->Age);
printf("P1's couple sex is %d\r\n", P1.Couple->Sex);
return 0;
}

本文展示了如何在C++中使用结构体定义Person类,并通过指针实现两个Person对象之间的引用关系。程序中,P1和P2对象互相引用对方作为伴侣。
2059

被折叠的 条评论
为什么被折叠?



