|
-
September 9th, 2005, 11:08 PM
#1
cannot declare a managed variable in unmanaged classs
guys,
how do you you all settle the above problem with a breeze..
i thought that __nogc class will give way to mix the unmanaged and managed types and declaration?
-
September 10th, 2005, 03:56 PM
#2
Re: cannot declare a managed variable in unmanaged classs
i thought that __nogc class will give way to mix the unmanaged and managed types and declaration?
The keyword __nogc on a class or struct indicates that it is an unmanaged C++ class or struct.
cannot declare a managed variable in unmanaged classs
To use manages types in unmanaged code you need to use the GCHandle struct.
-
September 10th, 2005, 06:50 PM
#3
Re: cannot declare a managed variable in unmanaged classs
To declare a managed variable reference in an unmanaged class you have to use the gcroot<> templated class e.g.
Code:
class CMyUnmanagedClass
{
public:
CMyUnmanagedClass(System::String *psString)
{
m_psString = psString;
}
void Test()
{
System::Diagnostics::Debug::WriteLine(m_psString);
}
private:
gcroot<String *> m_psString;
} ;
It behaves like a smart pointer i.e. it has overloads for ->, = etc etc (as shown in the Test() method above).
Okay ?
You can also use gcroot in STL declarations : this is how you can have a std::vector of String * (for instance) e.g.
Code:
std::vector<gcroot<String *> > asStrings;
Darwen.
Last edited by darwen; September 10th, 2005 at 06:53 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|