在 .cpp 文件中定义 C++ 命名空间方法的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8681714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to define C++ namespace methods in .cpp file
提问by Mr. Boy
Probably a duplicate, but not an easy one to search for...
可能是重复的,但不是一个容易搜索的...
Given a header like:
给定一个标题,如:
namespace ns1
{
class MyClass
{
void method();
};
}
I've see method()
defined in several ways in the .cpp file:
我已经method()
在 .cpp 文件中看到以多种方式定义:
Version 1:
版本 1:
namespace ns1
{
void MyClass::method()
{
...
}
}
Version 2:
版本 2:
using namespace ns1;
void MyClass::method()
{
...
}
Version 3:
版本 3:
void ns1::MyClass::method()
{
...
}
Is there a 'right' way to do it? Are any of these 'wrong' in that they don't all mean the same thing?
有没有“正确”的方法来做到这一点?这些“错误”中的任何一个是否都意味着同一件事?
采纳答案by GILGAMESH
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?)
版本 2 不清楚也不容易理解,因为你不知道MyClass
属于哪个命名空间而且它只是不合逻辑(类函数不在同一个命名空间中?)
Version 1 is right because it shows that in the namespace, you are defining the function.
版本 1 是正确的,因为它表明在命名空间中,您正在定义函数。
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.
版本 3 也是正确的,因为您使用了::
作用域解析运算符来引用MyClass::method ()
命名空间中的ns1
。我更喜欢版本3。
See Namespaces (C++). This is the best way to do this.
请参阅命名空间 (C++)。这是最好的方法。
回答by Puzomor Croatia
5 years later and i thought I'd mention this, which both looks nice and is not evil
5 年后,我想我会提到这个,它既好看又不邪恶
using ns1::MyClass;
void MyClass::method()
{
// ...
}
回答by Dietmar Kühl
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.
我使用第 4 版(如下),因为它结合了第 1 版(resoective 定义的简洁性)和第 3 版(最大程度明确)的大部分优点。主要缺点是人们不习惯它,但由于我认为它在技术上优于替代方案,因此我不介意。
Version 4: use full qualification using namespace aliases:
版本 4:使用命名空间别名使用完全限定:
#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).
在我的世界里,我经常使用命名空间别名,因为一切都是明确限定的——除非它不能(例如变量名)或者它是一个已知的自定义点(例如函数模板中的 swap())。
回答by Bjarke Freund-Hansen
Googles C++ Style Guidedictates your version 1, without indentation though.
Googles C++ Style Guide规定了你的版本 1,但没有缩进。
回答by Paul Jtheitroademan
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.
版本 3 使类和命名空间之间的关联非常明确,但代价是输入更多。版本 1 避免了这种情况,但捕获了与块的关联。版本 2 倾向于隐藏这一点,所以我会避免那个。
回答by jakumate
It turns out it's not only "coding-style matter". Num. 2 leads to linking error when defining and initializing a variable declared extern in header file. Take a look at example in my question. Definition of constant within namespace in cpp file
事实证明,这不仅仅是“编码风格的问题”。数量 2 定义和初始化在头文件中声明为 extern 的变量时会导致链接错误。看看我的问题中的例子。cpp文件中命名空间内常量的定义
回答by justin
I choose Num.3 (a.k.a. the verbose version). It's more typing, but the intent is exact to you and to the compiler. The problem you posted as-is is actually simpler than the real world. In the real world, there are other scopes for definitions, not just class members. Your definitions aren't very complicated with classes only - because their scope is never reopened (unlike namespaces, global scope, etc.).
我选择 Num.3(又名详细版本)。这是更多的打字,但意图对你和编译器来说是准确的。您按原样发布的问题实际上比现实世界更简单。在现实世界中,定义还有其他范围,而不仅仅是类成员。您的定义对于类来说并不是很复杂 - 因为它们的范围永远不会重新打开(与命名空间、全局范围等不同)。
Num.1 this can fail with scopes other than classes - anything that can be reopened. So, you may declare a new function in a namespace using this approach, or your inlines could wind up being substituted via ODR. You will need this for some definitions (notably, template specializations).
Num.1 这可能会因类以外的范围而失败 - 任何可以重新打开的范围。因此,您可以使用这种方法在命名空间中声明一个新函数,或者您的内联最终可能会被 ODR 替换。对于某些定义(特别是模板特化),您将需要它。
Num.2 This is very fragile, particularly in large codebases - as headers and dependencies shift, your program will fail to compile.
Num.2 这非常脆弱,尤其是在大型代码库中 - 随着头文件和依赖项的变化,您的程序将无法编译。
Num.3 This is ideal, but a lot to type - what your intent is to define something. This does exactly that, and the compiler kicks in to make sure you've not made a mistake, a definition is not out of synch with its declaration, etc..
Num.3 这是理想的,但要输入很多 - 您的意图是定义什么。这正是这样做的,并且编译器会启动以确保您没有犯错误,定义与其声明不同步,等等。
回答by Renan Greinert
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.
在版本 1 中,您的优点是不必在每个函数前编写命名空间。缺点是你会得到一个无聊的标识,特别是如果你有不止一层的命名空间。
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).
在第 2 版中,您使代码更简洁,但是如果在 CPP 中实现了多个命名空间,一个人可能会直接访问另一个人的函数和变量,从而使您的命名空间无用(对于那个 cpp 文件)。
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.
在版本3中,您将不得不输入更多,并且您的功能线可能比屏幕大,这对设计效果不利。
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 =]
由您来选择哪种更适合每种情况 =]