I was just reading a little bit on them from http://www.cplusplus.com/doc/tutorial/namespaces/ and it seems like a struct is capable of the same things? Or even a class for that matter. Maybe someone here can better define what a namespace is, and how it differs from a struct/class?
Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. All identifiers at namespace scope are visible to one another without qualification.
A struct (or class) that has only static data and methods is not so different from a namespace. But with a struct that has non-static data, you can create different objects with different data.
If all the member fields are value types. If instances of the type are small and short-lived or embedded to other instances. If it logically denotes a single value, same as primitive types like int, double, etc. If the size of the instance is below 16 bytes.
Namespace definitions cannot appear inside functions; that is, they must appear outside functions at global scope.
Well, seems everyone's going at it, so I'll add my own arguments.
First things first, namespace
and struct
are completely different beasts: they have different syntax and different semantics.
The obvious:
struct
introduces a type, you can use as templates argumentnamespace
can be spread in several filesSyntactically:
namespace ns = mylong::name::space;
and struct with typedef mylong::name::Space lilstruct;
Semantically:
namespace
only defines a scope for the definition of symbols, which allows to group together objects that work together (classes and free-functions) while isolating them for the rest of the world (name clashes). As such it often represents a logical unit of work within the project (for small projects, there is a single namespace).struct
or class
defines a logical binding between data and the methods to act upon it, which is the corner stone of encapsulation. It usually has one clear responsability and a number of invariants.Note that sometimes a struct
or class
is just used to bind together objects that work together without having any logic, for example struct Person { std::string name, firstName; };
.
That being said: there is no point in C++ for a struct
of static
methods. It's just a perversion from Java or C# and their "pure" OO approach. C++ supports free functions, so there is no point not using them, especially since it's better for encapsulation (they don't have access to private/protected parts, so you can't mess up an invariant and they don't depend on the representation of the class either).
Namespaces and class-types are not capable of the same things. Namespaces are mainly used to group types and functions together to avoid name collisions, while class-types hold data and operations that work on that data.
To just group functions and objects by using a class-types you'd have to make them static:
struct X {
static void f();
};
Without static
you'd have to create instances of the class-types to use them. A namespace is much better suited here:
namespace X {
void f();
}
Another important thing are using
declarations and directives:
namespace X {
void f();
void g();
}
void h() {
using X::f;
f(); // f() now visible in current scope
using namespace X;
f(); g(); // both visible
}
With class-types there simply is no mechanism that allows that.
What class-types give you over namespaces is that you can have multiple instances with differing state - if you need that use a class-type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With