Sign in to follow this  
mooreaa

inheritance - preventing access to functions

Recommended Posts

Hello, I have a question about inheritance. I inherit from a base class, but want to restrict some of public functions. class base { public: void foo(); void bar(); }; class myobj : public base { // I want to restrict access to function bar, but still allow calls to foo. } So that : base object; object.foo(); object.bar(); myobject obj2; obj2.foo(); obj2.bar(); // <- fail/disallow I need to inherit as public because in my case the base object actually is already deriving from something else. This might be a really basic question I just haven't run across it and havn't been able to find anything on it. Any ideas would be appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by mooreaa
I need to inherit as public because in my case the base object actually is already deriving from something else.


If you use public inheritance, then 'myobject' is a 'base', therefore it has a public 'bar' member. End of topic. This is true both on the language semantics level (static_cast<base&>(obj2).bar() will remain available with public inheritance) and on the design level (a derived class should always do what its base class does).

If you still insist on doing this despite the glaring limitations, you can always define a new function with the same name in the derived class:

class base { public: void foo() {} };
class derived : public base { void foo(); };

Share this post


Link to post
Share on other sites
Thanks ToohrVyk.

I'm working with an outside lib so I don't have too much control over whats in the base class. I'll have to see about braching out their classes and see how that works.

When I saw Knuckler comment on the "using" keyword I thought that would be perfect... but I guess it doesn't work the way I thought it would. Too bad you can't rescope things like that easily.

Share this post


Link to post
Share on other sites
You can also use private or protected inheritance...


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

class Sub: protected Base
{
public:
virtual void foo() { Base::foo(); }
void bing() { bar(); }
};


Sub and its subclasses can access Base's public and protected members as though they were protected in Base, and cannot access its private members (as usual). The public interface of Sub includes a new method bing() and a virtual override for foo() which calls the inherited foo().

So; you still have a foo() method, but keep in mind this means Sub does not implement the interface of Base; it only inherits its functionality for re-use.

Share this post


Link to post
Share on other sites
Quote:
Original post by mooreaa
Thanks ToohrVyk.

I'm working with an outside lib so I don't have too much control over whats in the base class. I'll have to see about braching out their classes and see how that works.


Why do you think you want to remove access to a base class function?

Have you considered using composition instead of inheritance?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this