0

I like to have well defined interface in a few classes, for this I created pure virtual functions in an abstract class that each class needs to implement.
But I'm facing the problem that I can instantiate the classes so I have to create another class that inherit the interface class, and all the other classes needs to inherit from this base class. Here is the code for example:

The interface

class Interface
{
     virtual std::string getName() = 0;
}

class Base : public Interface
{
   virtual std::string getName(return std::string("Base") ;)
}

class A : public Base
{
  std::string getName(return std::string("A") ;)

}

class B : public Base
{
  std::string getName(return std::string("B") ;)

}

All this so i could in code to have the same type for A & B.
Can I just use the Interface class without the Base class?
Something like this:

class A : public Interface
{
  std::string getName(return std::string("A") ;)

}
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 2
    what's the problem with the last A class? – Andrew Jul 05 '12 at 08:13
  • yes you can. no need to use a middle class. – Mohammad Jul 05 '12 at 08:13
  • by the way, you should declare virtual destructor in the interface. Otherwise you may receive a leak when deleting Base class through interface pointer – Andrew Jul 05 '12 at 08:13
  • 1
    What languages are you used to? There are very basic errors in your code, apart from the inheritance issues. – Mr Lister Jul 05 '12 at 08:13
  • 2
    `std::string getName(return std::string("A") ;)` that's some weird code btw. Did you mean `std::string getName(){ return std::string("A"); }`? – SingerOfTheFall Jul 05 '12 at 08:14
  • Are u sure this is C++? Which compiler you used to compile this? – PiotrNycz Jul 05 '12 at 08:15
  • @Andrew Do you suppose deleting through a pointer to the interface is supposed to work? My guess would be no, and if not, a protected nonvirtual destructor would be better than a public virtual destructor. –  Jul 05 '12 at 08:16
  • @hvd: It will work. There is no difference between Interface class or any other c++ class. – Andrew Jul 05 '12 at 08:18
  • @Andrew I'm not saying it won't work (with a public virtual destructor), I'm merely saying I expect it to be a programmer error that could be prevented at compile time by not having a public destructor. –  Jul 05 '12 at 08:19
  • I tried to edit the post to fix the English grammar, but you should probably start with a basic tutorial on C++ grammar. If you wrote that code correctly then you could compile it and learn for yourself whether you need the intermediate `Base` class. – Jonathan Wakely Jul 05 '12 at 09:06

3 Answers3

6

No they don't. All classes that inherit (directly or indirectly) from the interface must have the pure virtual function implemented somewhere in the inheritance tree in order to be instantiated.

So you can either implement the method in the base class, or your top class.

Also, your syntax is way off:

class A : public Interface
{
  std::string getName()
  {
      return std::string("A");
  }
};

The above should work even though it's not derived from Base, because you implement the pure virtual method from Interface.

Suggestion - make your getName method const.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

You cannot instantiate the interface, but what you can do is have a pointer to the interface, pointing to an instance of an implementation:

class Interface { /* pure virtual methods */ };
class A : virtual public Interface { /* implement all pure virtual methods */ };
class B : virtual public Interface { /* implement all pure virtual methods */ };

Interface * i0 = new A();
Interface * i1 = new B();

This is a standard way of using polymorphic classes (although you might want to use smart pointers instead of raw pointers).

Edit: concerning the use of virtual inheritance, see here and here for more information.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • It might help avoid confusing the OP if you explain why you changed the inheritance to virtual. – Jonathan Wakely Jul 05 '12 at 08:59
  • does all Interfaces have only pure virtual methods? – gaussblurinc Jul 05 '12 at 09:02
  • 1
    @loldop there is no concept of "interface" at the language level. C++ allows you to mix pure virtual, virtual and non-virtual methods. It is often a convention that if you call something an interface it has only virtual methods, but the language cannot enforce that. – juanchopanza Jul 05 '12 at 09:04
  • i think, that `interface` has only virtual methods with `public`-visible. but about purity of these methods: i'm not sure. – gaussblurinc Jul 05 '12 at 09:07
  • 1
    @loldop there is no `interface` in C++, so everything is by convention, and conventions change from place to place. – juanchopanza Jul 05 '12 at 09:09
0

You cannot create an instance of the base class if it has a pure virtual function as it is then an abstract base class. If you need to be able to instantiate this class don't have any pure virtual functions in it. Otherwise simply derive from it.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101