Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:43:49+00:00 2026-06-06T01:43:49+00:00

Suppose we have two classes: class Base { private: int x; public: void f();

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:43:56+00:00Added an answer on June 6, 2026 at 1:43 am

    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
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes: public class Student { public int Id {get; set;}
suppose you have two (or more) classes with private member vectors: class A {
Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose I have two classes defined the following way: public class A { public
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose I have two classes class A { b bInstance; public A ( b
I have two classes: class A { public: int i; }; class B :
Suppose I have two classes in a Rails application: class Subject < ActiveRecord::Base def
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose I have two classes a base class and an inherited class as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.