Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ derived class ever have to include definitions of inherited functions/members in header file?

Title pretty much says it all- do Derived classes have to re-include the definitions of functions/data members inherited from Parent classes in the header file?

Can the inherited functions/members be implied from the inheritance statement? Should they be explicitly re-declared for readability or could this risk hiding?

I am unsure what I should do in header files of derived classes.

like image 589
user997112 Avatar asked Nov 05 '13 22:11

user997112


2 Answers

Does C++ derived class ever have to include definitions of inherited functions/members in header file?

Taken very literally: no - in C++, the definition of any non-template function - irrespective of inheritance or class membership - need never appear in a header. Classes only define static data members - in their implementation file per the One Definition Rule - and the base-class definition needn't and can't be repeated in any derived class.

That said, I think you must be thinking of function declarations, which:

  • need not be repeated for non-virtual functions, though you may want a using statement to expose hidden base-class functions
  • must be repeated in the derived-class definition when that derived class overrides the base class's virtual function definition/implementation (with very limited variation allowed for a co-variant return type, and the ability to change default parameters - but don't do it!).

For example:

struct Base { virtual void f() = 0; };

struct Derived : Base { virtual void f(); };                // must mention f() here...

// then either in same header as Derived (and `inline` per One Definition Rule) or
// out-of-line in an implementation file...

?inline? void Derived::f() { std::cout << "Hello world\n"; }  // ...to define it here

If you don't mention f() inside struct Derived { }, the compiler will assume you're going to:

  • use the base class definition/implementation (if any / above example has = 0 for a pure virtual function so none's available)), or
  • specify an implementation later in an even-more-derived class/struct (if it's pure virtual, then this class will remain abstract and you can't create an object of this class type).

Can the inherited functions/members be implied from the inheritance statement? Should they be explicitly re-declared for readability or could this risk hiding?

For almost all cases this is covered by what I've said above (i.e. you only re-declare functions you'll provide new definitions for); the exception is for functions that are pure virtual in the base class and will remain pure virtual in your derived class... you can optionally declare them if you think it has some documentation value - it makes no difference at all.

I am unsure what I should do in header files of derived classes.

Normally you just want to specify:

  • additional functions and members,
  • using statements for any hidden functions you wish to expose
  • virtual functions that you'll provide new definitions for, whether inline in the derived class's header or out-of-line in its implementation file

You don't need to repeat data member definitions.

like image 128
Tony Delroy Avatar answered Oct 06 '22 01:10

Tony Delroy


Does C++ derived class ever have to include definitions of inherited functions/members in header file?

You need to specify the parent (base) class declaration via a header file. Inherited functions need not be redeclared.

Can the inherited functions/members be implied from the inheritance statement?

If I understand your question correctly, yes they are implied in the sense they need not be explicitly specified again.

Should they be explicitly re-declared for readability

As you're inheriting classes and not functions, re-declaration of functions is not required. Also, re-declaration may not always improve readability (it can clutter the file as well).

or could this risk hiding?

Information Hiding or Abstraction will not be affected as long as the access modifiers are not changed.

like image 40
Manav Kataria Avatar answered Oct 05 '22 23:10

Manav Kataria