3

I have a class Base and want to write a class Derive which inherits only some of the member functions from Base. Is there any convenient way to do this?

For example,

class Base
{
   ...
   void fooA();
   void fooB();
   ...
}

I wish to define a class Derive which inherits all members except fooB from Base.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
C. Wang
  • 2,516
  • 5
  • 29
  • 46
  • 1
    What do you mean by "derives"? – Medinoc Jan 08 '14 at 14:06
  • If you only have `fooB` in `Derived`, all its other members will be derived from `Base`. Do you mean "override"? – molbdnilo Jan 08 '14 at 14:06
  • The word "derive" is usually used when you talk about class relationships ("class A derives from class B so B is a base class). When we talk about functions, we use words "override" and "inherited": Class A overrides fooA but inherits fooB (IOW, fooA has a different implementation in class A but fooB stays the same). Could you rephrase your question with this in mind? –  Jan 08 '14 at 14:08
  • @molbdnilo The right opposite. – herohuyongtao Jan 08 '14 at 14:08
  • Your question makes no sense in its current form. You should clarify it. – juanchopanza Jan 08 '14 at 14:13
  • @juanchopanza: Made sense to me. He wants `Derived` to derive from `Base`, but to pick and choose which methods from `Base` are inherited in `Derived`. – John Dibling Jan 08 '14 at 14:18
  • @molbdnilo,@Medinoc, yes, derives means overrides. Actually this problem results from the fact that, I wanted to override parts of the functions in class `Base` but also found `fooA` was dangerous if it can be called from `Derived`. I want `fooA` invisible in class `Derived` while avoiding affecting `Base`. – C. Wang Jan 08 '14 at 14:25

3 Answers3

5

When you derive a subclass from a base class, you inherit all member functions from the base class (except the constructors & destructor, which are never inherited). You cannot pick and choose which members to derive.

However, this seems like an XY Problem. Depending on what you're really trying to do, you might be able to accomplish your actual goals.

You could change the visibility of a derived method:

class Derived : public Base
{
private: 
  using Base::fooB; // fooB() is now private
};

You could use private inheritance, and then make public only those methods you wish to provide:

class Derived : private Base
{
public:
  using Base::fooA;  // Only fooA() is public here
};

You could avoid inheritance altogether and encapsulate Base instead, and provide bridge methods for the resources in Base you wish to provide access for:

class Derived 
{
Base mBase; // private
public:
  void fooA() { mBase.fooA(); }
};

However, note that in the later two cases you lose some of the benfits of polymorphism. In order to have a Base* point to something that is actually a Derived, you must use public inheritance.

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Note that in the last two cases, a `Base *` cannot point to a `Derived` object. This is only possible for public inheritance, which establishes an *is-a* relationship – king_nak Jan 08 '14 at 14:14
  • If I remember C++ inheritance right you also loose polymorphism with the first case. If you declare a variable of Type Base and assign an instance of type Derived it won't call the derived method fooB but the one from the base class? Or would it even throw some error? – thuri Jan 08 '14 at 14:29
  • @thuri: There are no `virtual` methods here, so there is no polymorphism in the first place. But if there were, you could do `Base* p = new Derived;` and then call those virtuals, getting the polymorphic behavior you desire. I suspect you're remembering how polymorphism works incorrectly. – John Dibling Jan 08 '14 at 14:47
  • @John :-) I spent quite some time now in Java and C#. Of course you're right that virtual must be used in C++ (and C#) to get real polymorphism. But what i'm curios about is what would happen, if you specify fooB as virtual and make fooB private in a derived class? Would this bring up a compiler error? And if not what would be the runtime behaviour in that case? – thuri Jan 08 '14 at 14:58
  • Perfect answer, exactly the kind of answer I would look for. –  Oct 29 '20 at 16:43
0

If I am right, then what you are looking is Inheritance. Which means if A inherits B, it inherits B's methods and properties, if and only they are cleared protected or public.

Check these links:

  1. http://www.cplusplus.com/doc/tutorial/inheritance/
  2. http://www.youtube.com/watch?v=gq2Igdc-OSI
meWantToLearn
  • 1,691
  • 2
  • 25
  • 45
0

You can make fooB private and it will be invisible in the derived class. Learn about private/protected/public modifiers and about public/private/protected inheritance. These are tools you need.

  • Making fooB private works but also make it cannot be called by instance of class `Base`. But in my program, I don't want to do any damage things on class `Base`. Is there other ways to do that? – C. Wang Jan 08 '14 at 14:18