2

Inheritance of leaf classes

 2 years ago
source link: https://www.codesd.com/item/inheritance-of-leaf-classes.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Inheritance of leaf classes

advertisements

A class design guideline found in Sutter&Alexandrescu's coding standards book, among others, is to make base classes abstract, such that one cannot instantiate them. This is to prevent a.o. slicing and problems with polymorphic assignment and copying.

However, when using a class library like e.g. Qt, it is common practice to inherit from concrdete classes from the library to add additional behavior. For example one would create a class MyListBox which inherits from QListBox, adding application-specific behavior for all list boxes in my application.

How is can this common practice be reconciled with the advice to have non-instantiatiable base classes?


Your very first sentence contains the answer you are looking for:

A class design guideline found in Sutter&Alexandrescu's coding standards book

(emphasis mine).

It's just that - a guideline, not a rule carved in stone.

If you have specific technical limitations, for example in the library you are using, you may ignore it if the given alternative at that particular moment is way worse (like having to use some pattern that triples the total amount of code or having to rewrite the library).

The whole point of my answer: All these patterns, guidelines and best practices are means by which you can reach your programming goal. They are not the goal, themselves.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK