-2

My understanding is that abstract classes must have one or more pure virtual methods.

Can this class be considered abstract?

class B {
    protected:
        B() { }
    public:
        virtual ~B() { }
};

Finally, is the term abstract class defined in any of the recent C++ standards?

kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • 2
    [class.abstract] defines the term *abstract class*. – Kerrek SB Aug 16 '16 at 14:08
  • Your `B` can be instantiated: `struct D : B {}; B b = D();` – Kerrek SB Aug 16 '16 at 14:09
  • 2
    specifically, paragraph 2 of [class.abstract] specifically states ["An abstract class is a class that can be used only as a base class of some other class... A class is abstract if it has at least one pure virtual function"](http://eel.is/c++draft/class.abstract#2) – jaggedSpire Aug 16 '16 at 14:18
  • 1
    XY problem? I do not see how this question has any value. – SergeyA Aug 16 '16 at 16:42

3 Answers3

3

No, such a class cannot be considered abstract because (as mentioned in the comments, excerpt from the working draft):

A class is abstract if it has at least one pure virtual function.

Unfortunately, there are cases when one cannot add a pure virtual method to a class to turn it in an abstract one and still he doesn't want users to be able to instantiate that class.

For the sake of curiosity, I'm adding this answer to mention an often unknown technique to work around the issue.
Actually, you can easily turn such a class in an abstract one, even if you don't have any virtual method to be added to it.
The basic idea is to exploit the destructor declaration to do that.
A minimal, (not) working example follows:

struct B { virtual ~B() = 0; };
// keep in mind the ODR
B::~B() { }

int main() { B b{}; }

The code above won't compile with the error:

cannot declare variable 'b' to be of abstract type 'B'

Please, note that the definition of the destructor should be placed in a .cpp file, so as not to violate the ODR.


To be honest, I haven't found any case in which this technique can be used till now. Anyway, it's worth mentioning it for future readers.

skypjack
  • 49,335
  • 19
  • 95
  • 187
0

No, class B can not be considered as abstract.

class A {
    public:
        virtual void method() = 0; 
        virtual ~A() = 0;   
}

A is pure virtual, you cannot create object of A. You must create children class B which implements method after A.

0

abstract classes must have one or more pure virtual methods.

Exactly, and you class don't have it.

In accordance with this, a abstract class is a type which cannot be instantiated, but can be used as a base class (note: not "by"). In C++ this can be achieved with the usage of pure virtual method.

A pure virtual method is a virtual function whose declarator has the following syntax:

class B {
  virtual void foo() = 0;
}

Note: the syntax = 0 which indicates a pure virtual method. That simply means you don't have to specify an implementation for that method, and it cannot be possible to create any instance of that class (that is, a abstract class).

In conclusion your class B is not an abstract class.

Finally, is the term abstract class defined in any of the recent C++ standards?

The abstract class is a definition itself, and it's define as I've just mentioned before.

If you mean a specific defined syntax as, for example in Java (abstract class ...), then the answer is no. Again an abstract class in C++ is defined just with a class which has a pure virtual method.

BiagioF
  • 9,368
  • 2
  • 26
  • 50