Pregunta

First of all, I'm new to c++, and 'trying' to prefix my variables. But it isn't very clear to me. So my question is, is it correct to prefix static variables with "g_"? Thank you!

using namespace std;

// The main window class name.
static TCHAR g_szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR g_szTitle[] = _T("Win32 App");

...
¿Fue útil?

Solución 3

The most obvious definition of a global variable is a variable declared at namespace scope (including the outermost namespace).

Now, you could argue that a variable declared at namespace scope which is also declared static and, thus, isn't visible outside the given translation unit. Likewise, a variable declared in an unnamed namespace might be considered non-global. However, both of these kinds of variables shared many of the the bad properties of global variables. For example, they introduce a serialization point when being accessed from multiple threads.

Thus, I consider actually a wider range of variables to be global, i.e., also static data members in classes and function locale static variables. Each of these also exists just once throughout a a program. Just because these constructs happen to be used for some [anti] design patterns (notable Singleton) doesn't magically bless global variables!

With respect to prefixing variables names: do not include type prefix into your variable names! In C++ types are already sufficiently checked by the compiler. Including the type tends to result in eventually incorrect names. Specifically with respect to global variables, here is my recommendation for their prefix: whenever you want to use the prefix for a global variable stop whatever you are doing! You are in the process of constructing a problem and you should rather seek to change the design to remove the need for the global variable!

Otros consejos

It's better to use a prefix than nothing that distinguishes global variables as such. But

  • it's even better to avoid global variables to the degree possible, and

  • instead of a C style prefix, in C++ you can use a named namespace.

It also has many advantages to avoid Microsoft's T macro silliness. It's in support of Windows 9x, and you're probably not targeting Windows 9x. Also, it has many advantages, not the least for maintenance, to avoid Microsoft's silly Hungarian notation thing, that is, prefixes like sz, which was in support of Microsoft's 1980's Programmers Workbench help system, which just like Windows 98 is not very relevant any longer.

Also, it can be advantageous to use const wherever practically possible.

Note that const at namespace level implies static storage class, so an explicit static is then no longer necessary.

Thus, instead of the current

// The main window class name.
static TCHAR g_szWindowClass[] = _T("win32app");

do

namespace g {
    auto const windowClassName = L"win32app";
}

with

  • C++ namespace g instead of C prefix g_,

  • const added, guaranteeing that this variable is not modified, and

  • direct use of wide character literal instead of Microsoft Windows 9x T macros.

Then you can refer to g::windowClassName, or without the prefix after a using namespace g;, or even with an alias for g.

The particular braces convention I use for namespaces is in support of nested namespaces without the indentation hassle. Unfortunately that's not supported by common editors.

C++ has no official naming conventions. It does have a few rules for variable names, or identifers in general, which you have to follow, but other than that, names are entirely up to you, with all the flexibility and dangers it brings (much like the rest of the language).

Here is a good overview of the rules: http://en.cppreference.com/w/cpp/keyword

So, for example, _G_szTitle would be wrong, but g_szTitle is OK.

The real problem is that you almost certainly do not want to use globals. Global variables are almost always bad design. Avoid them.

Another, smaller, problem is that you use the so-called "Hungarian notation". Google a bit for it to find out why many people (myself included) are opposed to it, especially in a language like C++.

C++11 Standard (draft n3337):

17.6.4.3.2 Global names [global.names]

Certain sets of names and function signatures are always reserved to the implementation:

— Each name that contains a double underscore __ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.

— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

Other than these there aren't any restrictions on the (identifier) names you choose for global variables.

It's a convention used by some to prefix global variables by g_, member variables by m_, etc. This is a matter of choice; the language itself doesn't impose such a requirement. So you're free to name them anything and prefix them with anything as long as the identifier starts with an English alphabet.

As for the usage of global variables, I would say if you are just beginning to learn C++, use them, get hurt and then realize how they are bad; you'll see why they are always condemned by experienced programmers. Just telling they're bad would add little value, some things are better learned by experience.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top