1

Consider the following code:

class Base
{
  protected:
  virtual void methodDefinedInBase() = 0;
}

Class Derived: private Base
{
  public:
  void someMethod();
  protected:
  virtual void methodDefinedInBase()
  {
    std::cout<<"From B"<<std::endl;
  }
}

In the above code, I can create object of type "Derived". C++ allows me access to the method "methodDefinedInBase()" from "someMethod()" in Derived class. But, how do I create an object of type "Base" ?

Thanks,

Vishnu.

Vishnu Pedireddi
  • 2,142
  • 4
  • 25
  • 34
  • 2
    You can't. Why would you want to? – Beta Jun 21 '11 at 22:28
  • How exactly is methodDefinedInBase "defined" in Base? It is only declared there. – Andrei Jun 21 '11 at 22:34
  • I was trying to implement composition using private inheritance. And my natural impulse was to create an object of "Base" and add it to "Derived" class. But, I was defeating my own purpose. Implementing a private Inheritance would enforce Composition. :) – Vishnu Pedireddi Jun 21 '11 at 22:40

3 Answers3

3

As Beta noted in a comment, you can't instantiate an abstract base class (one with pure virtual methods.) You can only instantiate derived classes that implement those pure virtual methods. That's true regardless of whether you're using public or private inheritance.

Dan Breslau
  • 11,472
  • 2
  • 35
  • 44
1

You don't create any objects of type "Base" -- by giving Base a pure-virtual member, you are explicitly saying that this class cannot exist by itself, but only through derived classes. What you do want to create are pointers or references to Base:

Derived1 x;
Derived2 y;

// Somewhere inside Derived1:
Base & rb = x;

// Somewhere inside Derived2:
Base * pb = &y;

Then you can use polymorphism by treating rb and pb uniformly without needing to know the concrete type of x and y.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I'm resisting the urge to edit your answer to add `const` to the declarations for `rb` and `pb` :-) But, yeah, good examples. – Dan Breslau Jun 21 '11 at 23:17
  • @Dan: Why const? Can't I want to use non-const virtual methods, or doesn't that make sense? – Kerrek SB Jun 21 '11 at 23:49
  • except that OP is inheriting privately from `Base`, so he can't hold a reference to `Derived` in a `Base&` – Lambdageek Jun 22 '11 at 00:29
  • @Lambdageek: Ah, very interesting. I never realised that, I thought private inheritance just makes all the members of the base class private in the derived class. So how could I do this? Even "const Base & rb = x;" gives me an "inaccessible base" error. – Kerrek SB Jun 22 '11 at 00:45
  • @Kerrek SB -- certainly, you can, if you must. I just have a bias towards making `const` the default. (And please note the smilely in my earlier comment -- I *do* have a bias, but I wasn't seriously saying that they *had* to be const.) – Dan Breslau Jun 22 '11 at 01:59
  • @Lambdageek -- You could still use pointers or references to Base inside of Derived's implementation, or (I think) in friends of Derived. So the examples here are not wrong, just a bit limited in scope. – Dan Breslau Jun 22 '11 at 02:01
  • 1
    @Dan: Oh, I get it. Private inheritance limits the polymorphism to within the derived classes. Nice. I share your general desire to "const when possible, non-const when necessary", I just didn't want to obscure the example... – Kerrek SB Jun 22 '11 at 02:11
0

Declaration of pure virtual function in a base class means: 1. Object of such class can not be instantiated. 2. To instantiate object of derived class all pure virtual functions must be defined. In other words pure virtual method disallow you to create object of class where one defined.

user883041
  • 51
  • 1
  • 4