What kind of prefix do you use for member variables?

No doubt, it’s essential for understanding code to give member variables a prefix so that they can easily be distinguished from “normal” variables. I dispute this claim. It’s not the least bit necessary if you have half-decent syntax highlighting. A good IDE can let you write your code in readable English, and can show you … Read more

Can you start a class name with a numeric digit?

Rules for identifier names in C++ are: It can not start with a number Can be composed of letters, numbers, underscore, universal character names1 and implementation defined characters Can not be a keyword. The sections in the C++ draft standard that cover this are 2.11 Identifiers which includes the following grammar: identifier: identifier-nondigit <- Can … Read more

How to generate random variable names in C++ using macros?

Try the following: // One level of macro indirection is required in order to resolve __COUNTER__, // and get varname1 instead of varname__COUNTER__. #define CONCAT(a, b) CONCAT_INNER(a, b) #define CONCAT_INNER(a, b) a ## b #define UNIQUE_NAME(base) CONCAT(base, __COUNTER__) void main() { int UNIQUE_NAME(foo) = 123; // int foo0 = 123; std::cout << foo0; // prints … Read more

What’s the best approach to naming classes?

I’ll cite some passages from Implementation Patterns by Kent Beck: Simple Superclass Name “[…] The names should be short and punchy. However, to make the names precise sometimes seems to require several words. A way out of this dilemma is picking a strong metaphor for the computation. With a metaphor in mind, even single words … Read more