Difference between revisions of "Cpp Coding Standards/VIRTUAL"
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | == Virtual Classes (VIRTUAL) == | |
+ | ''Virtual classes are classes with (at least one) virtual functions.'' | ||
− | + | ===== 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. [[/LSP|-> Details]] | |
− | = | + | |
− | ==== 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. | + | |
− | + | ||
− | [[/LSP| | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | ==== | + | ===== 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. [[/Inst|-> Details]] | |
− | [[/NVI|Details]] | + | ===== Non Virtual Interface <span id="NVI">(NVI)</span> ===== |
+ | If a class has more than one parallel derived class, use the Non Virtual Interface idiom. [[/NVI|-> Details]] | ||
− | ==== | + | ===== Overrides <span id="Over">(Over)</span> ===== |
− | + | Give all overrides of a virtual function the same default values.<br> | |
+ | Use the “virtual” keyword also on all overrides. [[/Over|-> Details]] | ||
− | [[/ | + | ===== 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. <br> | ||
+ | Non-virtual mix-in classes need a protected destructor. [[/RightDestr|-> 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. [[/SafeCopy|-> Details]] | ||
---- | ---- | ||
− | === Related Rules === | + | ==== Related Rules ==== |
− | + | '''How to Inherit''' [[../Class Design#Inherit|-> CLSDESIGN:Inherit]] | |
---- | ---- | ||
[[Category:Coding Standards]] | [[Category:Coding Standards]] |
Latest revision as of 09:34, 23 May 2007
Contents
Virtual Classes (VIRTUAL)
Virtual classes are classes with (at least one) virtual functions.
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
Non Virtual Interface (NVI)
If a class has more than one parallel derived class, use the Non Virtual Interface idiom. -> Details
Overrides (Over)
Give all overrides of a virtual function 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
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
How to Inherit -> CLSDESIGN:Inherit