This article presents a naming convention inspired by Java that can be applied to the names of C++ classes.
In the Java language a convention exists in the naming of variables, methods, classes and constants. Given an identifier x, one can use the convention to distinguish whether x is the name of a class or one of the other three: variable or method or constant.
The convention is as follows:
The advantage of using this convention is that the names of classes can be easily distinguished from the rest of the code. The C++ language does not have this convention but in this article, I will explain how it can be usefully applied to C++.
Consider the following C++ example:
class X { //... }; void f(X x) { // ... } int main() { X xx; f(xx); } |
When objects of a class X are passed by-value to the function f as they are in the above example, a call to the constructor X(const X&) is generated every time f is called. If the class X contains no such constructor then the default behaviour of X(const X&) is for a memory copy to occur. In the above example where f is called using xx, a memory copy operation is generated to copy sizeof(X) chars from memory location &xx to memory location &x on the stack.
Sometimes the programmer may wish to suppress by-value argument passing for a class, perhaps because the operation is inefficient. This can be done by declaring X(const X&) in the class X without a body like so:
X(const X&); |
If by-value argument passing is ever used for X objects, a linker error will result from an attempted call to the non-existent constructor X(const X&). Better still, the constructor can be made private, like so:
class X { // ... private: // Disable passing by value: X(const X&); // ... }; |
resulting in a compile-time error if passing by value is ever attempted outside the X class and a linker error if passing by value is ever attempted inside the X class.
If passing an object by value is not to be used, it is also likely that returning an object by value should not be used. To disable returning an object by value, the method X& operator = (const X&) should be defined as private and without a method body, alongside the method X(const X&), like so:
class X { // ... private: // The following two lines disable passing // by value and returning by value: X(const X&); X& operator = (const X&); // ... }; |
A compile-time error will then result whenever a function is called that returns an X object by value outside the X class and a linker error will result when the same is attempted inside the X class.
Classes for which by-value argument passing is suppressed can then only be passed by-pointer or by-reference. The convention that I propose is as follows:
Add the following lines of Emacs Lisp code to your .emacs file to achieve correct syntax highlighting of capitalised classes:
;; The following code highlights capitalised classes: (kill-local-variable 'c++-font-lock-extra-types) (if (not (boundp 'c++-font-lock-extra-types)) (setq c++-font-lock-extra-types nil)) (setq-default c++-font-lock-extra-types (append '("[A-Z]" "[A-Z_]+[a-z][a-zA-Z0-9_]*") c++-font-lock-extra-types)) |
Back to Research Projects |
This page has the following hit count:
|