Correct way to define C++ namespace methods in .cpp file

后端 未结 8 1431
你的背包
你的背包 2020-12-12 11:10

Probably a duplicate, but not an easy one to search for...

Given a header like:

namespace ns1
{
 class MyClass
 {
  void method();
 };
}
相关标签:
8条回答
  • 2020-12-12 11:44

    I'm using version 4 (below) because it combines most of the advantages of version 1 (terseness of the resoective definition) and version 3 (be maximally explicit). The main disadvantage is that people aren't used to it but since I consider it technically superior to the alternatives I don't mind.

    Version 4: use full qualification using namespace aliases:

    #include "my-header.hpp"
    namespace OI = outer::inner;
    void OI::Obj::method() {
        ...
    }
    

    In my world I'm frequently using namespace aliases as everything is explicitly qualified - unless it can't (e.g. variable names) or it is a known customization point (e.g. swap() in a function template).

    0 讨论(0)
  • 2020-12-12 11:51

    5 years later and i thought I'd mention this, which both looks nice and is not evil

    using ns1::MyClass;
    
    void MyClass::method()
    {
      // ...
    }
    
    0 讨论(0)
  • 2020-12-12 11:52

    Googles C++ Style Guide dictates your version 1, without indentation though.

    0 讨论(0)
  • 2020-12-12 11:56

    Version 2 is unclear and not easy to understand because you don't know which namespace MyClass belongs to and it's just illogical (class function not in the same namespace?)

    Version 1 is right because it shows that in the namespace, you are defining the function.

    Version 3 is right also because you used the :: scope resolution operator to refer to the MyClass::method () in the namespace ns1. I prefer version 3.

    See Namespaces (C++). This is the best way to do this.

    0 讨论(0)
  • 2020-12-12 12:01

    Version 3 makes the association between the class and the namespace very explicit at the expense of more typing. Version 1 avoids this but captures the association with a block. Version 2 tends to hide this so I'd avoid that one.

    0 讨论(0)
  • 2020-12-12 12:02

    All the ways are right, and each one has its advantages and disadvantages.

    In the version 1, you have the advantage of not having to write the namespace in front of each function. The disadvantage is that you'll get a boring identation, specially if you have more than one level of namespaces.

    In version 2, you make your code cleaner, but if you have more than one namespace being implemented in the CPP, one may access the other one's functions and variables directly, making your namespace useless (for that cpp file).

    In version 3, you'll have to type more and your function lines may be bigger than the screen, which is bad for design effects.

    There is also another way some people use it. It is similar to the first version, but without the identation problems.

    It's like this:

    #define OPEN_NS1 namespace ns1 { 
    #define CLOSE_NS1 }
    
    OPEN_NS1
    
    void MyClass::method()
    {
    ...
    }
    
    CLOSE_NS1
    

    It's up to you to chose which one is better for each situation =]

    0 讨论(0)
提交回复
热议问题