Difference between revisions of "Cpp Coding Standards/CLSDESIGN"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
m
Line 1: Line 1:
 
General hints for class design. Items referring to constructors, destructors, copying and allocation (of classes) reside in topic [[../CLSINIT | Class Construction, Destruction and Copying]]. Items referring to virtual classes in hierarchies reside in [[../HIERARCHY | Hierarchies of Virtual Classes]]. Encapsulation related items are in topic [[../ENC | Encapsulation]].
 
General hints for class design. Items referring to constructors, destructors, copying and allocation (of classes) reside in topic [[../CLSINIT | Class Construction, Destruction and Copying]]. Items referring to virtual classes in hierarchies reside in [[../HIERARCHY | Hierarchies of Virtual Classes]]. Encapsulation related items are in topic [[../ENC | Encapsulation]].
 
----
 
----
== Summary ==
+
=== Summary ===
=== One Responsibility ===
+
==== [[#One Responsibility_2 |One Responsibility]] ====
 
Give one class only one cohesive responsibility.
 
Give one class only one cohesive responsibility.
  
=== Inherit ===
+
==== Inherit ====
 
Inherit to be reused, not to reuse. Else prefer composition over inheritance.
 
Inherit to be reused, not to reuse. Else prefer composition over inheritance.
  
=== No Implicit Conversions ===
+
==== No Implicit Conversions ====
 
Make single argument constructors “explicit”. Be aware of default arguments.
 
Make single argument constructors “explicit”. Be aware of default arguments.
 
Do not provide operator TYPE() functions.
 
Do not provide operator TYPE() functions.
  
=== Specific New and Delete ===
+
==== Specific New and Delete ====
 
When you provide a class-specific new or delete with custom parameters, provide the corresponding new or delete as well. (Not doing this causes memory leaks.)
 
When you provide a class-specific new or delete with custom parameters, provide the corresponding new or delete as well. (Not doing this causes memory leaks.)
 
If you provide one class specific new, this hides all other variants of new. So you probably want to provide also the three all of the standard forms of new (plain, nothrow and inplace).
 
If you provide one class specific new, this hides all other variants of new. So you probably want to provide also the three all of the standard forms of new (plain, nothrow and inplace).
----
+
 
+
----  
== Related Rules ==
+
=== Related Rules ===
 
* [[../CLSINIT | CLSINIT]] – all items
 
* [[../CLSINIT | CLSINIT]] – all items
 
* [[../CODEDOCU#ClassResponsibility  | CODEDOCU:ClassResponsibility]]
 
* [[../CODEDOCU#ClassResponsibility  | CODEDOCU:ClassResponsibility]]
Line 25: Line 25:
  
 
----
 
----
== Explanations ==
+
=== Explanations ===
=== One Responsibility ===
+
==== One Responsibility ====
Test text.
+
It is preferable to focus on one thing at a time. This avoids implementation problems with diverging requirements and allows easier learning for users and maintainers of the class.
 +
===== Example =====
 +
std::basic_string is a well known example of a class that wants to do too many things at once.
 +
 
 
----
 
----
 
[[Category:Coding Standards]]
 
[[Category:Coding Standards]]

Revision as of 17:59, 27 November 2006

General hints for class design. Items referring to constructors, destructors, copying and allocation (of classes) reside in topic Class Construction, Destruction and Copying. Items referring to virtual classes in hierarchies reside in Hierarchies of Virtual Classes. Encapsulation related items are in topic Encapsulation.


Summary

One Responsibility

Give one class only one cohesive responsibility.

Inherit

Inherit to be reused, not to reuse. Else prefer composition over inheritance.

No Implicit Conversions

Make single argument constructors “explicit”. Be aware of default arguments. Do not provide operator TYPE() functions.

Specific New and Delete

When you provide a class-specific new or delete with custom parameters, provide the corresponding new or delete as well. (Not doing this causes memory leaks.) If you provide one class specific new, this hides all other variants of new. So you probably want to provide also the three all of the standard forms of new (plain, nothrow and inplace).


Related Rules


Explanations

One Responsibility

It is preferable to focus on one thing at a time. This avoids implementation problems with diverging requirements and allows easier learning for users and maintainers of the class.

Example

std::basic_string is a well known example of a class that wants to do too many things at once.


Personal tools