Is there any project organization standard for C++?

Asked

Viewed 514 times

7

In Java and Actionscript3.0 we use namespaces based by directory path, I see much use of namespace, but are not based on the "location" path of the class in the folder.

I searched a lot if there was any kind of organization like that has that "recommendation" for PHP, the PSR-4, but for C++ I found no recommendation, I know it must be something trivial, but I believe a minimum of organization, regardless of the size of the team or being a personal project can be a good way, in case the purpose is not to use namespaces in all classes, only in isolated libs that I will reuse for various applications.

There is something like this for C++, an "official design standard"?

Since it doesn’t exist, I thought I’d do something like this:

  • ./fornecedor/categoria/foo.h

    namespace Fornecedor
    {
        namespace Categoria
        {
            class Foo()
            {
                public:
                    Foo();
            };
        };
    };
    
  • ./fornecedor/categoria/foo.cpp

    #include "foo.h"
    
    using namespace Fornecedor::Categoria;
    
    Foo::Foo()
    {
        ...
    }
    

In this example above:

  • Class name is associated with file name
  • Files and folders are always in lowercase (lower case letters)
  • Category would be only to divide the use of classes, for example fornecedor/matematica/soma.cpp and fornecedor/matematica/divisao.cpp

This is just an idea, it would be a good way?

  • Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

1 answer

7

The namespace of the C++ works the same way in C#. That is, they are only "surnames" for their members, nothing more than that.

One of the advantages of this is that they can be composed. You can join in it namespace completely isolated things, that do not know each other. So it is a mistake to try to use this mechanism the way presented in the question. Not that it can’t be done, but it wasn’t meant to be used like this.

It is conceptually wrong, and can prevent doing something in the future by having organized it this way. I would not go that way. It is not officially recommended not to do so, but it is also not recommended to do.

Note that the std works just the way I said. It is composed of several independent parts. It is to have few namespaces, is not to do the same as Java. Which by the way C# did not copy because they considered this problematic form.

What C++ has different from C# is that the latter encourages the hierarchy of names and C++ does not, it should be as simple as possible.

Note that the includesis what really determines the organization of what is each thing.

And now has the modules, which may be what you want. Depending on how you do it now, you may have difficulty using the new resource.

  • +1 excellent link, I’m reading the Design Choices, it’s gonna be a big step up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.