简体   繁体   English

C ++将类添加到名称空间:为什么?

[英]C++ adding class to a namespace: why?

I'm a newbie in c++. 我是C ++的新手。 Why, in Eclipse (configured with MinGW) and also in other threads, I noticed is used to add a class to a namespace? 为什么在Eclipse(使用MinGW配置)中以及在其他线程中,为什么我注意到是用来将类添加到名称空间的?

I provide an example to show you my actual doubt: 我提供一个示例来向您展示我的实际疑问:

#ifndef MODEL_MANGO_HPP_
#define MODEL_MANGO_HPP_

namespace std {

class Mango {
public:
    Mango();
    virtual ~Mango();
};

} /* namespace std */

#endif /* MODEL_MANGO_HPP_ */

EDIT : As shown in comments, it's completely forbidden to add classes to namespace std . 编辑 :如注释所示,完全禁止将类添加到命名空间std Quoting @owacoder, 引用@owacoder,

Namespaces are never closed, so you always have the ability to add class definitions to them. 命名空间永远不会关闭,因此您始终可以向其添加类定义。 However, by the specification the std namespace is to be considered closed. 但是,根据规范,std名称空间应视为封闭的。

To provide you a complete view of the context, here is the default implementation of the Mango.cpp, that Eclipse has done for me: 为了向您提供上下文的完整视图,这是Eclipse为我完成的Mango.cpp的默认实现:

#include "Mango.hpp"
namespace std {
Mango::Mango() {
    // TODO Auto-generated constructor stub
}
Mango::~Mango() {
    // TODO Auto-generated destructor stub
}
} /* namespace std */

So my question changes into: Why it's used "namespace std {...}" and when is a good practice to add classes to a namespace? 因此,我的问题变成: 为什么使用“命名空间std {...}”,什么时候将类添加到命名空间是一种好习惯?

You have to understand the basics of what classes and namespaces are. 您必须了解什么是类和名称空间的基础知识。

classes (along with structs, enums, and enum classes) are used to define user defined types in C++. 类(以及struct,enum和enum类)用于在C ++中定义用户定义的类型。

You create a class to represent a logical entity and encapsulate details, etc. 您创建一个类来表示逻辑实体并封装详细信息等。

namespaces are a way to mark territories of code and qualifying unique names for variables. 名称空间是一种标记代码区域和限定变量唯一名称的方法。

if you just write a class in a file, it will be written in the "global namespace" and it is not considered good practice because you are "polluting the namespace". 如果您仅在文件中编写类,则将其写在“全局名称空间”中,因为您正在“污染名称空间”,因此不认为是好的做法。

instead, you should use namespaces to limit the scope where your variable names have meaning. 相反,您应该使用名称空间来限制变量名具有含义的范围。 this way, you are not exhausting the pool of sensible class and variable names quickly (how many times have you wanted to write a "Utility" class?) 这样,您不会很快就耗尽所有明智的类和变量名(您想编写“实用程序”类多少次了?)

namespace firstNamespace{
int x=2;
}

namespace secondNamespace{
int x=7;
}

int main () 
{
std::cout << firstNamespace::x << '\n';
std::cout << secondNamespace::x << '\n';
return 0;
}

in this case, you can see that we can "reuse" the variable name x in different Contexts by qualifying a namespace. 在这种情况下,您可以看到我们可以通过限定名称空间在不同的上下文中“重用”变量名x。 inside the namespace blocks, we could have more declarations and definitions. 在名称空间块中,我们可以有更多的声明和定义。 including functions, classes, structs, etc. 包括函数,类,结构等

take not that namespaces remain open and you can add to them later. 不要以为命名空间保持打开状态,以后可以添加它们。 for example you can have this: 例如,您可以拥有:

namespace firstNamespace{
int x=2;
}

namespace secondNamespace{
int x=7;
}

namespace firstNamespace{
int y=11;
}

here, we added firstNamespace::y. 在这里,我们添加了firstNamespace :: y。

More importantly, you can observe that std is a namespace provided by C++ that contains a lot of useful variables, objects like cout which is of type std::ostream, functions and classeslike std::vector, std::ostream, etc. 更重要的是,您可以观察到std是C ++提供的名称空间,其中包含许多有用的变量,类型为std :: ostream的诸如cout之类的对象,函数以及诸如std :: vector,std :: ostream之类的类,等等。

so to go back to your question, the reason you want to wrap your class definitions in namespaces is to not pollute the global namespace. 因此,回到您的问题,您要将类定义包装在名称空间中的原因是不污染全局名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM