c、c++ 结构体的嵌套


/************************************************************************/
/* 嵌套结构体
* C++
**/
/************************************************************************/ struct A
{
private:
int a1[20];
public:
void initialize();
struct B
{
private:
A *b;
int *p;
public:
void initialize(A *a);
void next();
int read;
void set(int i);
};
friend A::B;
}; void A::initialize()
{
memset(a1, 0, sizeof(a1));
}
void A::B::initialize(A *a)
{
b = a;
p = a->a1; } void A::B::set(int i)
{
*p = i;
}
/*
void main()
{
A a;
A::B b1,b2;
int i;
a.initialize();
b1.initialize(&a);
b2.initialize(&a); for (i =0; i<20; i++)
{
b1.set(i);
b2.set(i);
}
} /************************************************************************/
/* C
* 嵌套结构体
*
* */
/************************************************************************/ typedef struct A
{
int year;
int month;
int day;
}; typedef struct B
{
string name;
int age; union{
A aa;
}xy; struct A aa1;
}; void main()
{
B *b1 =NULL;
b1->xy.aa.day =1991;
b1->aa1.month = 7; getchar(); }

05-28 15:22