Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a class able to access only certain private members of another class?

Tags:

c++

class

friend

Suppose we have two classes:

class Base
{
private:
    int x;
public:
    void f();
};

class Foo
{
    // some variables and methods
};

Now everyone can call Base::f(), but I want only Foo to be able to do so.

In order to achieve this effect, we can make Base::f() private and declare Foo as a friend:

class Base
{
private:
    int x;
    void f();
    friend Foo;
};

The problem with this approach is that Foo has the access to both Base::f() and Base::x (and even to any other private members of Base). But I want Foo to have access only to Base::f().

Is there a way for a class (or a function) to grant an access only to certain private members of another class? Or maybe anyone could suggest a better approach to my problem?

EDIT:

I'll try to specify the access restriction I need. Firstly, Base is an interface in a library (it's an abstract class, in fact). The user uses only the classes derived from Base. Base::f() is called only by Foo which is another class in the library. Hiding Base::f() from the user is important, because only Foo knows when to call it. At the same time, Foo shouldn't mess up the other members of Base.

like image 300
miloszmaki Avatar asked Jun 15 '12 15:06

miloszmaki


People also ask

How to access a private member of a class from other class?

How to Access a Private Member of a Class From Other Class 1 Access the private member using reflection 2 Access the private member using base by sub-class 3 Access the private member by methods. More ...

How to access private/protected method outside a class in C++?

How to access private/protected method outside a class in C++. Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access ...

Can We declare a class as private in Java?

Yes, we can declare a class as private but these classes can be only inner or nested classes. We can’t a top-level class as private because it would be completely useless as nothing would have access to it. can abstract class have final methods in java?

How to get all members of a class in a package?

First of all, import the class. Create an object of that class. Using this object access, the members of that class. Suppose there is a class in a package called myPackage with a method named display () You can access it as.


1 Answers

Very hacky, but this will allow very fine grained access.

class Base
{
private:
    int x;
    void f();
    friend class Base_f_Accessor;
};

class Base_f_Accessor
{
private:
    static void f(Base & b) { b.f(); }
    friend class Foo;
}

class Foo
{
    // some variables and methods
};
like image 77
Benjamin Lindley Avatar answered Sep 20 '22 02:09

Benjamin Lindley