inheritance - preventing access to functions

Started by
6 comments, last by Zahlman 15 years, 8 months ago
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.
Advertisement
class base
{
public:
void foo();

protected: // only derived classes can call
void bar();

private: // only base can call

};
Another way is:
class base{public:     void Foo();     void Bar();};class derived : private base{public:     using base::Foo;};
I don't think either of you read his question.
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(); };
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.
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.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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?

This topic is closed to new replies.

Advertisement