0

I do not want that it's possible to make an object of this class, but in my particular case, I don't know how to prevent this.

My idea to make this possible was:

class abstract
{
  private:
    int foo;

  public:
    abstract();
    virtual void useless_method_to_force_class_to_be_abstract() = 0;
    virtual ~abstract();
};

But this is really not best practice I suppose...

Are there any tips to build a concept so that the case "I don't know what method I should set to pure virtual" never happens?

Tim Klein
  • 2,538
  • 15
  • 19
alfa98
  • 21
  • 2
    You really can't have a class with zero methods that's abstract because it's considered complete, there's nothing to override. Abstract really just means it's incomplete, that you must fill in certain methods for it to be complete. What's your ultimate goal here, abstract class concerns aside? – tadman Apr 05 '19 at 16:29
  • 8
    If you don't want users to construct instances of this class, just make the constructors `protected`. – Quentin Apr 05 '19 at 16:30
  • what is the class good for if you cannot create instances of it? I mean there are many use cases that come to my mind but in none of them there would be any harm in creating an instance, it would be useless but not dangerous – 463035818_is_not_an_ai Apr 05 '19 at 16:35
  • 2
    Possible duplicate of [C++ abstract class without pure virtual functions?](https://stackoverflow.com/questions/14631685/c-abstract-class-without-pure-virtual-functions) – πάντα ῥεῖ Apr 05 '19 at 16:37
  • Thank you for the fast reply. My purpose is to have an abstract "prototype" of something and then be able to derive from this prototype to get objects with certain characteristics. But it should not be possible to instantiate this super class. In my case I don't actually know which method of the parent class should be set to "pure virtual". – alfa98 Apr 05 '19 at 16:41
  • 2
    *"derive from this prototype to get objects with certain characteristics"* And how do you change these characteristics? Normally this is done by overriding virtual functions. – HolyBlackCat Apr 05 '19 at 16:42
  • @HolyBlackCat Yes, but these don't need necessarily to be _pure virtual_ using an _abstract base (super) class_. – πάντα ῥεῖ Apr 05 '19 at 16:46
  • ...and your virtual functions either have a "default" implementation in the base or they are pure virtual (aka abstract). So either it is fine to create objects or the class is already abstract – 463035818_is_not_an_ai Apr 05 '19 at 16:47

1 Answers1

5

There is no need to introduce an unnecessary virtual member functions. Make the destructor pure virtual. Make sure to make the constructor protected to allow derived classes to be constructed. While at it, make the destructor also protected.

class abstract
{
   private:
      int foo;

   protected:
      abstract();
      virtual ~abstract() = 0;
};

Please note that it's OK to implement pure virtual functions. In the case of destructors, they must be implemented even when they are pure virtual.

R Sahu
  • 204,454
  • 14
  • 159
  • 270