C++ 类和命名空间之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3188156/
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
Difference between classes and namespaces?
提问by TimothyTech
I'm looking at namespaces and I don't really see a difference between these and classes. I'm teaching myself c++ I've gotten several books online, so i know I'm not learning the most effectively. Anyways can someone tell me the difference between the two and what would be the best time to use a namepace over a class. Also, I don't see much about structs in the book I'm reading.
我正在查看名称空间,但我并没有真正看到它们与类之间的区别。我在自学 C++ 我在网上买了几本书,所以我知道我没有最有效地学习。无论如何,有人可以告诉我两者之间的区别以及在类上使用命名空间的最佳时间。另外,我在我正在阅读的书中没有看到太多关于结构的内容。
Is this the format?
这是格式吗?
struct go
{
goNow(){ cout << "go Now"};
}
thanks in advance for your assistance.
提前感谢你的帮助。
回答by Cogwheel
Classes and structs define types. You can create an object of a type. Namespaces simply declare a scope inside which other types, functions, objects, or namespaces can exist. You can't create an object of type std
(unless of course you created a type called std
, which would hide the std
namespace).
类和结构定义类型。您可以创建一个类型的对象。命名空间只是声明一个作用域,在该作用域内可以存在其他类型、函数、对象或命名空间。你不能创建一个类型的对象std
(除非你创建了一个名为 的类型std
,它会隐藏std
命名空间)。
When you define a function inside a struct/class (a method) you're saying "This function is a fundamental operation on the associated data". When you define a function inside a namespace you're saying "This function is logically related to other functions, types, and objects in the namespace"
当您在结构/类(方法)中定义函数时,您是在说“此函数是对关联数据的基本操作”。当你在命名空间内定义一个函数时,你是在说“这个函数在逻辑上与命名空间中的其他函数、类型和对象相关”
Edit
编辑
It's probably worth pointing out that "everything is an object" languages like Java and C# regularly use classes as if they were namespaces because they don't allow "free" functions. This may be where the confusion comes from. If you have a class in another language that contains nothing but static members, you would want to use a namespace and free functions in the C++ version.
可能值得指出的是,像 Java 和 C# 这样的“一切都是对象”语言经常将类用作命名空间,因为它们不允许“自由”函数。这可能是混乱的来源。如果您有一个用另一种语言编写的类,它只包含静态成员,那么您可能希望在 C++ 版本中使用命名空间和自由函数。
回答by Abhay
You can search on the web for the differences and i am sure you will find many; but the following are important IMHO:-
您可以在网上搜索差异,我相信您会发现很多;但以下是重要的恕我直言:-
- You can reopen a namespace and add stuff across translation units. You cannot do this with classes.
- Using a class implies that you can create an instance of that class, not true with namespaces.
- You can use using-declarations with namespaces, and that's not possible with classes unless you derive from them.
- You can have unnamed namespaces.
- 您可以重新打开命名空间并跨翻译单元添加内容。你不能用类来做到这一点。
- 使用类意味着您可以创建该类的实例,而不是使用命名空间。
- 您可以将 using 声明与命名空间一起使用,而这对于类是不可能的,除非您从它们派生而来。
- 您可以拥有未命名的命名空间。
A namespace defines a new scope and members of a namespace are said to have namespace scope. They provide a way to avoid name collisions (of variables, types, classes or functions) without the inconvenience of handling nested classes.
命名空间定义了一个新的作用域,命名空间的成员被称为具有命名空间作用域。它们提供了一种避免名称冲突(变量、类型、类或函数)的方法,而无需处理嵌套类的不便。
回答by bta
A classis a data type. If you have a class named Foo
, you can create objects of class Foo
and use them in many ways.
甲类是一种数据类型。如果您有一个名为 的类Foo
,您可以创建类的对象Foo
并以多种方式使用它们。
A namespaceis simply an abstract way of grouping items together. Normally, you cannot have two functions in your program named bar()
. If you place them in separate namespaces, then they can coexist (for example, as A::bar()
and B::bar()
). A namespace cannot be created as an object; think of it more as a naming convention.
一个命名空间是简单的分组物品放在一起的一种抽象的方式。通常,您的程序中不能有两个名为bar()
. 如果将它们放在单独的命名空间中,则它们可以共存(例如, asA::bar()
和B::bar()
)。命名空间不能作为对象创建;将其更多地视为一种命名约定。
If you are writing code that you want to be associated with an objectthat you can define and use as a variable, write a class. If you are writing an API or library and you want to wrap up all of the functions and constants so that their names don't clash with anything that the user might have written, use a namespace.
如果您正在编写希望与可以定义并用作变量的对象相关联的代码,请编写一个类。如果您正在编写 API 或库,并且想要包装所有函数和常量,以便它们的名称不会与用户可能编写的任何内容发生冲突,请使用namespace。
回答by Ragster
A namespace is a way of grouping identifiers so that they don't clash.
命名空间是一种对标识符进行分组的方式,这样它们就不会发生冲突。
A class is defeinition of an object that can be instantiated (usually) and which encapsulates functionallity and state.
类是可以实例化(通常)并封装功能和状态的对象的定义。
Namespaces and classes are entirely different, and serve different purposes. They have some syntactic similarity.
命名空间和类完全不同,用于不同的目的。它们在句法上有一些相似之处。
Structs are classes, with a different default access specifier (public for struct, private for class) - in all other aspects they are the same.
结构是类,具有不同的默认访问说明符(结构是公共的,类是私有的)——在所有其他方面它们都是相同的。
回答by Ragster
One major difference is that namespaces can be re-opened, but classes cannot be:
一个主要区别是命名空间可以重新打开,但类不能:
namespace A {
int f1();
}
namespace A {
int f2();
}
is legal, but:
是合法的,但是:
class A {
int f1();
};
class A {
int f2();
};
is not
不是
回答by Prasoon Saurav
From wikipedia
来自维基百科
In general, a namespace is an abstract container providing context for the items (names, or technical terms, or words) it holds and allowing disambiguation of homonym items having the same name (residing in different namespaces). As a rule, names in a namespace cannot have more than one spelling, that is, its components cannot share the same name. A namespace is also called a context, as the valid meaning of a name can change depending on what namespace applies. Names in it can represent objects as well as concepts, whether it is a natural or ethnic language, a constructed language, the technical terminology of a profession, a dialect, a sociolect, or an artificial language (e.g., a programming language).
一般而言,名称空间是一个抽象容器,为其持有的项目(名称、或技术术语或单词)提供上下文,并允许消除具有相同名称(位于不同名称空间)的同音项目的歧义。 通常,命名空间中的名称不能有多个拼写,即其组件不能共享相同的名称。命名空间也称为上下文,因为名称的有效含义可以根据应用的命名空间而改变。其中的名称既可以表示对象,也可以表示概念,无论是自然语言还是民族语言、构造语言、专业技术术语、方言、社会方言或人工语言(例如,编程语言)。
On the other hand A class defines a type
.
另一方面A class defines a type
。
A namespace may contain multiple classes.
一个命名空间可能包含多个类。
EDIT
编辑
One difference would be this:
一个区别是:
You can have unnamed namespaces but you can't have a unnamed class
您可以拥有未命名的命名空间,但不能拥有未命名的类
namespace{ //fine
//some code....
}
class{ //illegal
}