Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

friend ref class?

I have a managed class Parser and an unmanaged class CParser. The former is exposed from a DLL for managed clients (C# world). The core functionality is in the unmanaged class CParser. Since both are in the same DLL, and there is a one-to-one relationship, the managed class can access the unmanaged class.

The problem is that I want Parser to access all members of CParser. I don't want to use public: for all members, nor I am willing to have setters and getters. Thus, I just placed friend keyword:

class CParser
{
   ...
   friend ref class Parser; // C2144
};

// public ref class Parser{}; 

I get error C2144, probably for obvious reason that unmanaged C++ doesn't understand ref keyword. If I remove ref keyword, the managed compiler (compiling Parser), would complain with error C2872: Ambigious Symbol.

It is also known that internal: is not applicable for unmanged C++.

How to make Parser a friend of CParser ?

EDIT: This question was already here, but probably the unmanaged class is compiled under /clr. I cannot/will not compile unmanaged class using managed compiler.

like image 920
Ajay Avatar asked Apr 02 '13 05:04

Ajay


People also ask

What do you mean by friend class?

Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.

What is the difference between friend function and friend class?

Friend function is a function that is able to access the private and protected members of a class. In contrast, a friend class is a class which help in accessing the private members of a class. A friend function is declared by including its prototype inside the class, antecede it with the keyword friend.

What does friend mean in CPP?

A friend function in C++ is defined as a function that can access private, protected and public members of a class. The friend function is declared using the friend keyword inside the body of the class.

How would you define a friend function outside class?

The friend function definition is found outside the class like a normal member function. The friend function is not defined using the friend keyword or use the scope resolution operator :: as it is not a member of the class in which it has been declared. A friend function can be declared in several classes.


1 Answers

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);
}
like image 142
Tim Sylvester Avatar answered Oct 11 '22 11:10

Tim Sylvester