C++内部类( Inner Class )

// CBird.h: interface for the CBird class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CBird_H__E26DE3B0_DA3A_46D5_BAE3_CE06E1021A6A__INCLUDED_)
#define AFX_CBird_H__E26DE3B0_DA3A_46D5_BAE3_CE06E1021A6A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class IFly
{
public:
	virtual void Fly() = 0;
};

class ISwim
{
public:
	virtual void Swim() = 0;
};

class CBird  
{
public:
	CBird();
	virtual ~CBird();

	class XFlyObj:public IFly
	{
	public:
		CBird * m_pParent;
		void Fly();
	} m_XFlyObj;

	class XSwimObj:public ISwim
	{
	public:
		CBird * m_pParent;
		void Swim();
	} m_XSwimObj;

	int m_nData;
};

#endif // !defined(AFX_CBird_H__E26DE3B0_DA3A_46D5_BAE3_CE06E1021A6A__INCLUDED_)

 

// CBird.cpp: implementation of the CBird class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CBird.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace std;

CBird::CBird()
{
	m_XFlyObj.m_pParent = this;
	m_XSwimObj.m_pParent = this;
	m_nData = 123;
}

CBird::~CBird()
{

}

void CBird::XFlyObj::Fly()
{
	cout << "I can fly! parent->m_nData:" << m_pParent->m_nData << endl;
}

void CBird::XSwimObj::Swim()
{
	cout << "I can Swim! parent->m_nData:" << m_pParent->m_nData << endl;
}

  

// HelloVC6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TestInnerClass.h"

int main(int argc, char* argv[])
{

	/*
	long *buffer;
	size_t size;
	
	if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
		exit( 1 );
	
	size = _msize( buffer );
	printf( "Size of block after malloc of 1000 longs: %u\n", size );
	buffer[999] = 987;
	printf("第999位数为%ld\n",buffer[999]);
	
	//* Reallocate and show new size: 
	if( (buffer = (long *)realloc( buffer, size + (1000 * sizeof( long )) )) 
        ==  NULL )
		exit( 1 );
	size = _msize( buffer );
	printf( "Size of block after realloc of 1000 more longs: %u\n", 
		size );
	printf("after realloc:第999位数为%ld\n",buffer[999]);

	
	free( buffer );
	*/
	
	CBird tic;
	tic.m_XFlyObj.Fly();
	tic.m_XSwimObj.Swim();
	tic.m_nData = 321;
	tic.m_XFlyObj.Fly();
	tic.m_XSwimObj.Swim();

	//system("pause");
	return 0;
}

  

 

 

posted @ 2016-01-03 19:55  庚武  Views(849)  Comments(0)    收藏  举报