Categories
Computers & Software

On Namespaces — Again!

I have a thing about C++ namespaces, and this is yet another post on the topic. Oh well, so my life isn’t that exciting…

Namespaces avoid polluting the global namespace with names that might conflict with each other. Classes similarly encapsulate method and data member names from the big bad cold outside global namespace, but without namespaces, the class names themselves are sitting out there in the open. So 2 classes named EditDialog will conflict. Put each one in a different namespace, and you’re ok. You’ll have to qualify the classes with the namespace name if you are going to use them together, e.g.

namespace A {
class EditDialog {};
}
namespace B {
class EditDialog {};
}

namespace C {
A::EditDialog d1;
B::EditDialog d2;
}

EP Simulator is programmed in C++ using the Qt library. All Qt classes are in the global namespace; the only saving grace there is that they all begin with the capital letter Q (e.g. QWidget). Lately I have been impressed with the Qt Creator IDE, provided free of charge by Nokia (who now owns Qt). The only problem I have is that the program is a bit stymied by namespaces. This is a result of my programming conventions. In my header files I wrap my class(es) in a namespace. It is bad to use the forms using A::EditDialog; or using namespace A; in a header file, because those names will pollute every source (cpp) file that includes that header. Also, you can’t use using A::EditDialog; inside the class definition, because it means something else in that context (it means to use a base class method and has nothing to do with namespaces there). So, the class is wrapped in the namespace and that’s fine. In my source (cpp) files, each method is decorated with the class name, e.g. EditDialog::edit() {} which is just how C++ works. One could precede each of these definitions with the namespace, e.g. A::EditDialog::edit() however, preceding all these definitions with a using A::EditDialog; statement seems to me to be the best way to limit namespace pollution. Interestingly, other variables inside the namespace are automatically looked up due to a phenomenon known as Koenig lookup or ADL, so you don’t have to have a using declaration for every variable in the namespace. Since source files are not included within other files, these using declarations don’t pollute anything else the way a header file would. The problem is, this confuses the Qt Creator editor, and nice things like code completion and switching header and source files doesn’t work right. The only thing that works is wrapping the source definitions in the namespace like the header file. I might end up doing that, but for now I am just hoping the Qt Creator folks will find a way to make all the features work for my style of namespace usage.

By mannd

I am a retired cardiac electrophysiologist who has worked both in private practice in Louisville, Kentucky and as a Professor of Medicine at the University of Colorado in Denver. I am interested not only in medicine, but also in computer programming, music, science fiction, fantasy, 30s pulp literature, and a whole lot more.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.