使用“namespace foo {”而不是明确限定在头文件之外 [英] Using "namespace foo {" instead of explicitly qualifying outside of header files

查看:78
本文介绍了使用“namespace foo {”而不是明确限定在头文件之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果函数声明为这样:

  namespace foo {
void bar
}

大多数人定义函数:

  void foo :: bar(){
...
}
 命名空间foo {
void bar(){
...
}
}


b $ b

我喜欢这种风格,因为它保存我从总是retyping foo ::,这是常常乏味的函数参数接受在同一命名空间中声明的类型。此外,它更容易重命名整个命名空间。



我想知道为什么我几乎从来没有看到我的风格在其他人的代码,有什么缺点,它,除了额外 如果你使用缩进级别(你甚至不必缩进命名空间)?

解决方案< foo :: bar 形式,并且意外地使用
定义的函数不正确的参数,你会得到一个编译器错误。如果将
定义放在源文件中的命名空间中,不同的参数
只会导致定义不同的函数。你不会得到一个
错误,直到你尝试链接到使用你的函数的代码(在
的情况下的DLL,可能不是在运行时)。


If a function is declared as such:

namespace foo {
    void bar();
}

Most people define the function like this:

void foo::bar() {
    ...
}

However I like to do it this way:

namespace foo {
    void bar() {
        ...
    }
}

I prefer this style because its saves me from always retyping foo::, which is often tedious in function parameters that accept types declared in that same namespace. Plus its easier to rename the whole namespace.

I'm wondering why I almost never see my style in other peoples code, are there any disadvantages too it, besides the additional indentation level (and you don't even have to indent namespaces)?

解决方案

If you use the foo::bar form, and accidentally define the function with incorrect arguments, you will get a compiler error. If you put the definition in a namespace in the source file, different arguments will simply result in a different function being defined. You won't get an error until you try to link with code which uses your function (which in the case of a DLL, may not be until runtime).

这篇关于使用“namespace foo {”而不是明确限定在头文件之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆