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 8383219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:06:44+00:00 2026-06-09T17:06:44+00:00

How to limit access to class function from main function? Here my code. class

  • 0

How to limit access to class function from main function?

Here my code.

class Bar
{
public: void doSomething(){}
};

class Foo
{
public: Bar bar;
//Only this scope that bar object was declared(In this case only Foo class)
//Can access doSomething() by bar object.
};

int main()
{
    Foo foo;
    foo.bar.doSomething(); //doSomething() should be limited(can't access)
    return 0;
}

PS.Sorry for my poor English.


Edit:
I didn’t delete old code but I expand with new code.
I think this case can’t use friend class. Because I plan to use for every class. Thanks

class Bar
{
public:
    void A() {} //Can access in scope that object of Bar was declared only
    void B() {}
    void C() {}
};

class Foo
{
public:
    Bar bar;
    //Only this scope that bar object was declared(In this case is a Foo class)
    //Foo class can access A function by bar object

    //main function need to access bar object with B, C function
    //but main function don't need to access A function
    void CallA()
    {
        bar.A(); //Correct
    }
};

int main()
{
    Foo foo;
    foo.bar.A(); //Incorrect: A function should be limited(can't access)    
    foo.bar.B(); //Correct
    foo.bar.C(); //Correct
    foo.CallA(); //Correct
    return 0;
}
  • 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-09T17:06:45+00:00Added an answer on June 9, 2026 at 5:06 pm

    Make Foo a friend of Bar

    class Bar
    {
        friend class Foo;
    private:
        void doSomething(){}
    };
    

    And also avoid making member variables public. Use setters/getters instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP page I need to limit execution access of to only
I want to limit the access of protected methods to certain inherited classes only.
I want my function to access an outside variable—from its parent function specifically. However,
I'd like to limit access to some specific PDF files but am having a
I have a main function in A.cpp which has the following relevant two lines
I have a class A which has public methods and used by 100 other
I have a base class and several subclasses derived from that base class. I
I've been wrestling with this code for a while, trying to clean it up
Guava is very useful. Now, I want to implement a limit for access counter
I know this must be a common question, but take a look: Here i

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.