c、c++ 結構體的巢狀

簡書成研發表於2014-08-05

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();

}


相關文章