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

  • SEARCH
  • Home
  • 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 842853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:01:55+00:00 2026-05-15T06:01:55+00:00

For example, class Base has two public methods: foo() and bar() . Class Derived

  • 0

For example, class Base has two public methods: foo() and bar(). Class Derived is inherited from class Base. In class Derived, I want to make foo() public but bar() private. Is the following code the correct and natural way to do this?

class Base {
   public:
     void foo();
     void bar();
};

class Derived : public Base {
   private:
     void bar();
};
  • 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-05-15T06:01:56+00:00Added an answer on May 15, 2026 at 6:01 am

    Section 11.3 of the C++ ’03 standard describes this ability:

    11.3 Access declarations
    The access of a
    member of a base class can be changed
    in the derived class by mentioning its
    qualified-id in the derived class
    declaration. Such mention is called an
    access declaration. The effect of an
    access declaration qualified-id ; is
    defined to be equivalent to the
    declaration using qualified-id

    So there are 2 ways you can do it.

    Note: As of ISO C++ ’11, access-declarations (Base::bar;) are prohibited as noted in the comments. A using-declaration (using Base::bar;) should be used instead.

    1) You can use public inheritance and then make bar private:

    class Base {
    public:
        void foo(){}
        void bar(){}
    };
    
    class Derived : public Base {
    private:
        using Base::bar;
    };
    

    2) You can use private inheritance and then make foo public:

    class Base {
    public:
        void foo(){}
        void bar(){}
    };
    
    class Derived : private Base {
    public:
        using Base::foo;
    };
    

    Note: If you have a pointer or reference of type Base which contains an object of type Derived then the user will still be able to call the member.

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

Sidebar

Related Questions

I have two case classes that inherit from an abstract base class. I want
For example: I have an implementation (say doSomething()) in Base class. I want all
Why can't I cast a base class instance to a derived class? For example,
I have two classes that are derived from an abstract generic class, which is
I've a class Foo<T> which has a vector of smart pointers to Shape derived
I've inherited a long term maintenance code base that has two radically different iPhone
I have a model, for example : class Account < ActiveRecord::Base before_create :build_dependencies def
All the code examples always use base() as follows class A { public A()
Example: class C { public: void operator =(int i) {} }; class SubC :
I have a derived class that's a very thin wrapper around a base class.

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.