c++バンドパラメータ構築グローバル変数実装方法

718 ワード

次のコードを参照してください.
#include "stdafx.h"
#include 
#include 

class Test
{
public:
	int a;
	int b;

	Test();
	Test(int c);  //        
 	~Test();
};


Test::Test(int c)
{
	a = c;
}

Test * createTestObject()
{
	Test *tmp = new Test(2);
	return tmp;
}

Test * testmain; //        
int _tmain(int argc, _TCHAR* argv[])
{
	testmain = createTestObject();
	std::cout << "get the result: " << testmain->a << std::endl;
	system("PAUSE");
	return 0;
}

実装方法:
1.グローバルのポインタを定義し、ポインタによってグローバルのオブジェクトにアクセスします.
2.createの関数を実装してオブジェクトを作成し、もちろんオブジェクトを破棄する方法も実装します.
3.オブジェクトを作成するstaticインタフェースをclassで実装することもできます.