可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
Is this the format?
struct go
{
goNow(){ cout << "go Now"};
}
thanks in advance for your assistance.
回答1:
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).
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.
回答2:
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.
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.
回答3:
A class is a data type. If you have a class named Foo
, you can create objects of class Foo
and use them in many ways.
A namespace is 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.
If you are writing code that you want to be associated with an object that 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.
回答4:
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.
回答5:
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
回答6:
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 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
}