Difference between revisions of "Cpp Coding Standards/VIRTUAL"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
m
Line 2: Line 2:
 
These items refer to class hierarchies where the base class is virtual and the derived classes are intended to be used – at least sometimes – via pointers or references to the base class.
 
These items refer to class hierarchies where the base class is virtual and the derived classes are intended to be used – at least sometimes – via pointers or references to the base class.
 
----
 
----
=== Rules ===
+
=== Summary ===
 
+
 
==== Liskov ====
 
==== Liskov ====
 
Always follow the Liskov Substitution Principle: Everything a client can do with a base class, it must be able to do with each derived class as well.  
 
Always follow the Liskov Substitution Principle: Everything a client can do with a base class, it must be able to do with each derived class as well.  
Line 29: Line 28:
 
=== Related Rules ===
 
=== Related Rules ===
 
* [[../CLSDESIGN#Inherit | CLSDESIGN:Inherit]]
 
* [[../CLSDESIGN#Inherit | CLSDESIGN:Inherit]]
 +
----
 +
=== Explanations ===
 +
 
----
 
----
 
[[Category:Coding Standards]]
 
[[Category:Coding Standards]]

Revision as of 18:16, 27 November 2006

Hierarchies of Virtual Classes

These items refer to class hierarchies where the base class is virtual and the derived classes are intended to be used – at least sometimes – via pointers or references to the base class.


Summary

Liskov

Always follow the Liskov Substitution Principle: Everything a client can do with a base class, it must be able to do with each derived class as well.

Instantiation

In an inheritance tree of virtual classes, instantiate only the leaves. Make all other classes either abstract or their constructors protected, so instantiation is impossible.

Overrides

Do not give overrides of a virtual function stronger preconditions or weaker postconditions.

Give them the same default values.

Use the “virtual” keyword also on all overrides.

RightDestructor

All virtual classes need a virtual destructor. If the class can not be instantiated, the destructor may be inline, else it has to be non-inline. Non-virtual mix-in classes need a protected destructor.

NonVirtualInterfaces

If a class has more than one parallel derived class, use the Non Virtual Interface idiom.

SafeCopying

If a virtual class needs to be copied via references to a base class, disable the copy constructor and operator=() in the base classes and implement the clone() idiom instead.


Related Rules


Explanations


Personal tools