简体   繁体   English

为什么要在名称空间中使用类

[英]why to use classes inside namespace

I wish to know the actual reason behind writing the class names inside namespace. 我想知道在名称空间内编写类名称的真正原因。 What purpose do they solve? 他们解决什么目的? Do they include the classes or do something else? 他们包括课程还是做其他事情? Any help is greatly appreciated. 任何帮助是极大的赞赏。

namespace ns3 { 
class Channel;
class SpectrumChannel;
class MyMod;                        ;

class NewModule : public NetDevice
{   
public:
// methods and data
}

It is good practice to put your classes into a namespace in order to avoid a name collision with other code (libraries) you might use (also coming in later in a project). 最好将类放在命名空间中,以避免与您可能使用的其他代码(库)发生名称冲突(也出现在项目的后面)。 When using common names like Vector , Logger , etc. as class names it can easily happen that they are also used in other code you want to use. 当使用诸如VectorLogger等的通用名称作为类名称时,很容易发生这种情况,即它们也在您要使用的其他代码中使用。 When you put your classes into a (well considered) namespace, this chance of a name-collision is minimized. 当您将类放入(经过深思熟虑的)名称空间中时,发生名称冲突的机会就降到了最低。

Forward declarations are a different (independent) topic. 前向声明是一个不同的(独立的)主题。 When classes are defined within a namespace, also their forward declarations must be done within that namespace. 当在名称空间中定义类时,它们的前向声明也必须在该名称空间中完成。

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

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