GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


A naming convention for C++ classes Free Stuff

Abstract

This article presents a naming convention inspired by Java that can be applied to the names of C++ classes.

1. Introduction

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:

  1. If x is all capitals and at least two letters, and possibly with underscores, then x names a constant.
  2. Otherwise if x starts with an initial capital, then x names a class.
  3. Otherwise x names a variable or method. Unfortunately the convention alone does not allow us to distinguish between these two.

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++.

2. Passing Data by Value

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.

3. The Naming Convention

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:

  1. If by-value argument passing and by-value returning is intended for the class, then the class should be named with an initial lowercase letter.
  2. If by-value argument passing and by-value returning is not intended for the class, then the class should be named with an initial capital letter.
This convention mirrors the situation in Java:
  1. Built-in types are passed and returned by-value. The built-in types are named with an initial lowercase letter.
  2. Class types are passed and returned by-reference. Class types are named with an initial capital letter.

6. Emacs Syntax Highlighting

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:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:11:33 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to