1
class ClassA {
public:
    ClassA() {} // when this function must be provided
    virtual ~ClassA() = 0 {}
};

class ClassB : public ClassA
{
    // ...        
};

I want to know when the default constructor of an abstract base class must be provided.

q0987
  • 34,938
  • 69
  • 242
  • 387
  • 1
    I don't think being a base abstract class has anything to do with it. So I'd guess the answer would be "whenever it's normally needed," such as when you need to be able to construct your class with and without arguments. – Seth Carnegie Aug 28 '11 at 02:36
  • 1
    Also you can't have a body on a pure virtual function (`= 0 { }`). – Seth Carnegie Aug 28 '11 at 02:39
  • 1
    @Seth Carnegie: [Actually, you can have a body on a pure virtual function](http://www.gotw.ca/gotw/031.htm). (The section called "*1. Pure Virtual Destructor*" is particularly relevant here.) – In silico Aug 28 '11 at 02:44
  • @In wow, what the blazes. What difference on earth does the `= 0` make then? I thought it meant subclasses _must_ implement it because the base class doesn't. – Seth Carnegie Aug 28 '11 at 02:47
  • @Seth Carnegie: The classes will still have to override all pure virtual functions before they can be instantiated, as always. But you can still provide a definition for subclasses to call. Read the linked GotW #31 article to see why someone might do that. – In silico Aug 28 '11 at 02:48
  • @In Wow thanks, C++ still surprises me. There is _so_ much I don't know, I don't know why I even try to answer questions. – Seth Carnegie Aug 28 '11 at 02:50
  • @Seth Carnegie: You should definitely read through [the Guru of the Week articles](http://www.gotw.ca/gotw/). :-) And don't stop trying to answer questions! You've provided lots of good ones already. – In silico Aug 28 '11 at 02:51
  • @In silico: you can't have a body in the declaration of a pure virtual, but you can have a separate definition. – Cheers and hth. - Alf Aug 28 '11 at 03:08
  • @In silico: also, be aware that the GOTW articles are now dated and many are downright incorrect, even in the sense of code that will not compile with any modern compiler. Also, that Herb is not maintaining them and is not responding to the mail address that he posts for GOTW correspondence. The GOTW series started as postings to [comp.lang.c++], and then, at that time and for those articles that were posted, had an extreme degree of peer review and corrections and could be relied on; that is no longer the case. – Cheers and hth. - Alf Aug 28 '11 at 03:12
  • The default constructor is not must. As you have done, definition for the pure virtual destructor is a must. – Jagannath Aug 28 '11 at 03:17
  • @Alf P. Steinbach: Many of them still have relevant info, and I'll link to those as appropriate. But you have a good point (especially w.r.t. peer review). – In silico Aug 28 '11 at 05:19

2 Answers2

2

there is no connection between providing a default constructor, and the abstractness or not of the class.

provide a default constructor definition if you need to initialize things.

provide a (possibly non-implemented) non-public declaration if you want to prohibit default construction.

cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

If it is truly an abstract base class with no data members, the compiler-generated constructor will be totally sufficient in every case.

The derived classes will always call the default base class constructor unless their constructor specifies a different one in the initializer list.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622