Oleg Shirokikh
Oleg Shirokikh

Reputation: 3565

Accessing private C++ methods within C++/CLI wrapper

Given a class method in C++ that has to be private there, how to make C++/CLI access it. Example: C++ class has a private constructor that shouldn't be exposed to the external user, C++/CLI managed code has to have similar private constructor, but internally it must access private C++ constructor to instantiate a member pointer referring to C++ native class.

Upvotes: 0

Views: 1403

Answers (1)

Matthew Moss
Matthew Moss

Reputation: 1248

Keep in mind, first of all, the goal of making things inaccessible (i.e. private) generally contradicts the goal of making things accessible. If you're able to change the class declaration, then you have some options. I don't know the specifics of your C++/CLI requirements, but perhaps one of these options will give you what you need.

Protected and Inheritance

To an "outsider", private and protected members are equally inaccessible. But to a subclass, private is still private while protected members are accessible. You can gain access to protected members by subclassing. This can be one way to unit test "private" members of a class without fully exposing it to the world.

class A {
protected:  // was private
    int sum(int a, int b);
};

class TestA : public A {
public:
    bool testSum(int expected, int a, int b) {
        return expected == sum(a, b);
    }
};

So access to protected method sum in class A becomes accessible by making it protected and subclassing. The private member will remain inaccessible to everything else except subclasses.

Static Factory Method

You mentioned a private constructor; if you are needing to manage construction, you might accomplish that with a static factory method. For instance:

class Foobar {
private:
    Foobar() { }  // Don't let anyone just make one.
public:
    static Foobar* Create() { return new Foobar; }
};

// To create a Foobar instance:
Foobar* pfoo = Foobar::Create();

This is not the best interface (better to use a shared_ptr, for instance), but it demonstrates my meaning. Note that since the factory function Create is public, anyone can create one. However, you can change the body of Create to something more complex: include bookkeeping, initialization, etc. The factory method is a way to manage creation, not restrict it. Granted, you may not want this, but it's an option if you need to manage construction.

Friends

C++ permits giving access of the private parts of classes to other classes/functions via the friend keyword.

class A {
    int x;  //private
    friend class B;
};

class B {
    void foobar(A& a) {
        a.x = 42;  // permissible, because A declared class B a friend
    }
};

Friendship can be granted to another class or to a function. This can easily break encapsulation and make code difficult to follow or keep safe, but it also gives you the means to expose private members to exactly just those who need it.

Upvotes: 1

Related Questions