Click to See Complete Forum and Search --> : Create a managed class object in MFC


piyumali2004
March 12th, 2009, 02:47 AM
Hi,

I'm developing an MFC application. I'm developing it using Visual Studio.net 2008 and I have set the option Common Language Run Time Support(/clr) in my application.In my application there is a managed C++ class called RTPRenderer. My main Dlg class of the MFC application is called MyPhnDlg.

I need to create an object of RTPRenderer in the MyPhnDlg class. First I have done it in the MyPhnDlg.cpp class (within a function) as below, with out any problem.

RTPRenderer ^myRTPRenderer = gcnew RTPRenderer();

But the problem is this myRTPRenderer object is visible to that function only. I need to use the same object in two different functions. So I thought to declare it in MyPhnDlg.h file as the same way above. Then it gives errors as below.

error C3265: cannot declare a managed 'myRTPRenderer' in an unmanaged 'CMyPhnGuiDlg'

error C2864: 'CMyPhnDlg::myRTPRenderer' : only static const integral data members can be initialized within a class

So can anyone please tell me how can create managed objects within a unmanaged class by declaring them in the header file?

cilu
March 14th, 2009, 07:57 PM
Take a look at gcroot<>.

piyumali2004
March 15th, 2009, 11:35 PM
Thanks lot for the help. I was able to do that using gcroot as follows.

gcroot<RTPRenderer ^> myRTPRenderer;