开发者

Modules Design in C++

I'm new in this website and I want to ask you some questions about C++ because I'm learning it these days and I'm a noob (I just know C and Java).

I'm using the Stroustrup book but I still don't understand the uses and differences between classes and namespaces.

Also I did some C programs for a subject (operating systems) and I want to join all of them in a C++ program. The idea I have is to creat some modules (because the programs are rely on but they are for different purpose). And also I have a "main.cpp" file where it's defined a help function which is called several times by the modules, check if the module is available and if so, you can choose the function of the module you want (depending on the parameter you passed).

But the problem is that I don't know what's the better way to do that. What should I use? classes or namespaces? and how can I do a good design? I thought about using some design as the following:

main.cpp

--> ModuleA.cpp (implement InterfaceA.h)

--> ModuleB.cpp (implement InterfaceB.h)

--> ModuleC.cpp (implement InterfaceC.h)

I was sear开发者_运维问答ching on this web some other similar questions about it and I found something like this one but I think it's too complicate for my simple program.

Thank you very much :)


You won't get far with just namespaces on their own :) All they do (as their name suggest) is provide a named-space for types to prevent pollution of the global namespace, and prevent conflict between identical type names. Therefore it is common for an API to have an outer namespace containing everything it has, thereby preventing conflicts with other APIs:

namespace MoosLibrary
{
    typedef boost::uuids::uuid Uuid;  // Moo's unique identifier!
};



namespace AnotherLibrary
{
    typedef int Uuid;          // this library uses integers for unique identifiers
};

In the above snippet, without namespaces, the two Uuid types would clash causing untold compilation errors and programming head scratching. However, by compartmentalizing them in to separate namespaces, we can avoid these kinds of type conflict. Note in this above example, boost::uuids::uuid. There are two namespaces there.. boost is the boost C++ library (highly recommended when you get to that level of C++!), and uuids is the namespace within boost dedicated to that particular part of the library.

So, from this we can see that not only are namespaces good for preventing conflict between types, it also allows you to structure your projects in to logical namespaces so that the global namespace isn't full of types. This is really neat when your IDE supports intellisense.

namespace MoosLibrary
{
    namespace Logic
    {
        // logic related classes/types
    }

    namespace UI
    {
        // user-interface
    }
}

Note: detail is a namespace that has an unwritten-rule associated with it. It's basically where API writers put stuff that needs to be publicly visible, but not screwed with.


If you know Java, then just think of a C++ namespace as a Java package, and a C++ class as a Java class.

Generally speaking, it's best to have one class per source file. If you have related classes, you may want to have them all in the same namespace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜