Difference between revisions of "Cpp Coding Standards/VIRTUAL"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Cpp Coding Standards/Topics/HIERARCHY moved to Cpp Coding Standards/Virtual Classes: Improve Cpp_Coding_Standards structure.)
Line 1: Line 1:
== Hierarchies of Virtual Classes ==
+
Topic-Id: '''VIRTUAL'''
 +
 
 
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.
 
----
 
----
 
=== Summary ===
 
=== Summary ===
==== Liskov ====
+
==== Liskov Substitution Principle <span id="LSP">(LSP)</span> ====
 
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.  
  
==== Instantiation ====
+
[[/LSP|Details]]
 +
 
 +
==== Instantiate Only Leaves <span id="Inst">(Inst)</span> ====
 
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.
 
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 ====
+
[[/Inst|Details]]
 +
 
 +
==== Overrides <span id="Over">(Over)</span> ====
 
Do not give overrides of a virtual function stronger preconditions or weaker postconditions.  
 
Do not give overrides of a virtual function stronger preconditions or weaker postconditions.  
  
Line 16: Line 21:
 
Use the “virtual” keyword also on all overrides.
 
Use the “virtual” keyword also on all overrides.
  
==== RightDestructor ====
+
[[/Over|Details]]
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 ====
+
==== Right Destructor <span id="RightDestr">(RightDestr)</span> ====
 +
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.
 +
 
 +
[[/RightDestr|Details]]
 +
 
 +
==== Non Virtual Interfaces <span id="NVI">(NVI)</span> ====
 
If a class has more than one parallel derived class, use the Non Virtual Interface idiom.
 
If a class has more than one parallel derived class, use the Non Virtual Interface idiom.
  
==== SafeCopying ====
+
[[/NVI|Details]]
 +
 
 +
==== Safe Copying <span id="SafeCopy">(SafeCopy)</span> ====
 
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.
 
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.
 +
 +
[[/SafeCopy|Details]]
  
 
----
 
----
 
=== Related Rules ===
 
=== Related Rules ===
* [[../CLSDESIGN#Inherit | CLSDESIGN:Inherit]]
+
* [[../Class Design#Inherit |CLSDESIGN:Inherit]] - How to Inherit
----
+
=== Explanations ===
+
  
 
----
 
----
 
[[Category:Coding Standards]]
 
[[Category:Coding Standards]]

Revision as of 22:59, 29 November 2006

Topic-Id: VIRTUAL

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 Substitution Principle (LSP)

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.

Details

Instantiate Only Leaves (Inst)

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.

Details

Overrides (Over)

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.

Details

Right Destructor (RightDestr)

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.

Details

Non Virtual Interfaces (NVI)

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

Details

Safe Copying (SafeCopy)

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.

Details


Related Rules


Personal tools