views:

157

answers:

6

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?

A: 

In C++ a struct is exactly the same as a class, except structs are public by default. Classes are private. So whenever you want to group free functions together use a namespace. When you want to group data and functions, use a struct/class, and optionally a namespace around it all.
Notice that If you put your functions in a struct then you would have to have an instance of your struct when you want to call those functions, unless they are static.

Chris H
"When you want to group data and functions, use a struct/class, and optionally a namespace around it all": _Wrong_. That's not how you choose between the two.
wilhelmtell
A: 

A class more than this - it is an "object" containing not only member variables but methods too. In addition, objects can be inherited, parent methods can be overloaded. You can have Classes are very powerful, but are only part of C++. You should allocate classes with new and destroy with delete to ensure the constructor and destructor methods are called. Also, to have a collection of classes dynamically resizeable you should use vector from the standard library.

From Brian's comment on this post:

Structs exactly the same as classes, except their default access rules are public. You can have member functions, static functions, and even private data and functions in a struct.

I didn't know structs could do that, apparently they can. However, structs in C cannot have methods (functions) although they can contain function pointers. I don't know if structs have constructors in C++ though.

This site seems to be a reasonably good tutorial of encapsulation, polymorphism and inheritance.

Now, namespaces. Namespaces are like a collection of classes, effectively. Why would you want to do this? Well, if you have a class named String in your program, you have named a class in such a way that it conflicts with the standard library. To solve this problem and allow you to use the same name for classes (rather than the C solution of mylibary_classname), namespaces were introduced.

Ninefingers
Structs exactly the same as classes, except their default access rules are public. You can have member functions, static functions, and even private data and functions in a struct.
Brian Neal
@Brian Neal: Edited your comment in as a quote - thanks, I didn't realise that - their capabilities are extended beyond C's structs, clearly.
Ninefingers
Even unions can have functions, constructors and private members in C++, because they are likewise class-types.
Johannes Schaub - litb
A: 

When creating your own library, it's normally good practice to namespace all your exported functions and classes.

That way, if someone includes your library, they won't be polluting their namespace, and there is less likelyhood of name clashes.

Brendan Abel
+1  A: 

If it can be done with a namespace, use a namespace.

A struct does much more than defining a scope. It defines a type.

Pavel Radzivilovsky
I find nested namespaces really annoying when they're not consisted. For example, I hate that some libraries in Boost have their own nested namespaces, while others don't because it isn't uniform. Why is it ok to say `boost::bind()` but you have to say `boost::assign::list_of()`? Either _always_ put a project in a namespace of its own, or _never_ do -- so your users will intuitively use your names correctly on the first try.
wilhelmtell
+5  A: 

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.

Georg Fritzsche
On top of that, structs and namespaces were meant for solve different problems. So when you use a namespace you communicate something else about what you try to achieve than when you use a struct.
wilhelmtell
+1  A: 

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:

  • a struct introduces a type, you can use as templates argument
  • a namespace can be spread in several files

Syntactically:

  • both can be "aliased", namespace with namespace ns = mylong::name::space; and struct with typedef mylong::name::Space lilstruct;
  • ADL (or Argument Dependent Lookup) is tailored for namespaces

Semantically:

  • a 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).
  • a 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).

Matthieu M.