[Step. 07] 비관리 클래스에서 관리 클래스를 멤버로, 관리 클래스에서 비관리 클래스를 멤버로
1. 비관리 클래스에서 관리 클래스를 멤버로
비관리 클래스에서 관리 클래스를 멤버로 가지고 싶을 때는 ‘gcroot’라는 템플릿을 사용합니다.
#include "stdafx.h"
#include <iostream>
#include <vcclr.h>
using namespace System;
class TEST
{
public:
TEST() {}
~TEST() {}
gcroot< String^ > m_str;
};
int main(array<System::String ^> ^args)
{
TEST test;
test.m_str = gcnew String("Hello VSTS 2010");
Console::WriteLine( test.m_str);
getchar();
return 0;
}
‘gcroot’를 사용하기 위해서는
#include <vcclr.h>
를 포함해야 합니다.
비관리 클래스에서 관리 클래스인 String을 다음과 같이 멤버로 선언합니다.
그리고 사용하기 위해서는 할당을 합니다.
test.m_str = gcnew String("Hello VSTS 2010");
2. 관리 클래스에서 비관리 클래스를 멤버로
관리 클래스에서 비관리 클래스를 멤버로 가질 때는 비관리 클래스를 포인터로 선언하여 비관리 힙에 동적할당을 합니다.
#include "stdafx.h"
#include <iostream>
#include <vcclr.h>
using namespace System;
class TEST
{
public:
TEST() {}
~TEST() {}
};
ref class refTEST
{
public:
refTEST() {}
~refTEST() {}
TEST* test;
};
int main()
{
refTEST^ refTest = gcnew refTEST();
refTest->test = new TEST;
return 0;
}
참고
http://msdn.microsoft.com/ko-kr/library/481fa11f%28v=VS.80%29.aspx
http://blog.naver.com/scor7910/40048083284