unmanaged class declaring managed member function as friend
-
i'm trying to accomplish something like this...
class ManagedClass;
class UnmanagedClass
{
protected:
void SetValue( std::string val );
friend void ManagedClass::SetValue( String* val );
};namespace MyNamespace {
class __gc ManagedClass
{
protected:
void SetValue( String* val );
};
}i can't get it to compile. i have tried just about every combination of the namespace and class name in the forward declaration and friend function definition. anyone have any ideas?? thanks.
-
i'm trying to accomplish something like this...
class ManagedClass;
class UnmanagedClass
{
protected:
void SetValue( std::string val );
friend void ManagedClass::SetValue( String* val );
};namespace MyNamespace {
class __gc ManagedClass
{
protected:
void SetValue( String* val );
};
}i can't get it to compile. i have tried just about every combination of the namespace and class name in the forward declaration and friend function definition. anyone have any ideas?? thanks.
I don't think you really need that friend directive. Most probably, you could simply make your classes public and avoid inline methods. It seems that you want to call a managed method from an unmanaged class. However, this can't be done by a direct call, because managed objects are subject to garbage collection. To get a safe entry point into the managed heap, you have to use a GCHandle, which in turn is wrapped by the gcroot template. Try this:
#include <vcclr.h>
class ManagedClass;
class UnmanagedClass
{
public:
UnmanagedClass( ManagedClass* p );
private:
gcroot< ManagedClass* > m_pManaged;
}You can then make your calls into the managed class with the m_pManaged pointer. There might be another problem: Since you have both managed and unmanaged code in one project, this project needs to be linked in mixed mode. This is because for the unmanaged code, you need the C runtime, which requires an entry point as well as statics. For an instruction of how to convert your project to mixed mode, see the article: "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the MSDN.