You can't use friend
in that case. The only way I have found to accomplish it is to declare another friend which gives free access to the private members, but is defined within the source file of the other class and thus cannot be used by any other code.
This is ugly in its own way but at least the ugliness is hidden in the cpp file, and the public interface of the native class remains relatively clean.
NativeClass.h
class NativeClass
{
public:
void PublicStuff();
struct DispatchHelper;
protected:
friend DispatchHelper;
void NonPublicStuff();
};
RefClass.h
ref class RefClass
{
public:
void ManagedCode();
};
RefClass.cpp
struct NativeClass::DispatchHelper
{
static void DoStuff(NativeClass* p) { p->NonPublicStuff(); }
}
void RefClass::ManagedCode()
{
NativeClass::DispatchHelper::DoStuff(pNativeClass);
}