views:

247

answers:

6

Im looking at namespaces and i dont really see a difference between these and classes. im teaching myself c++ ive gotten several books online, so i know im 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 dont see much about structs in the book im reading.

is this the format?

struct go
{

  goNow(){ cout << "go Now"};

}

thanks in advance for your assistance

+17  A: 

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.

Cogwheel - Matthew Orlando
+1. Regarding structs: `class` and `struct` are essentially the same thing - they are often both called "user-defined types". The only difference is that `class` makes members and base classes private by default, while `struct` makes them public.
Mike Seymour
+4  A: 

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
    }
Prasoon Saurav
I don't really see how the quoted text relates to c++.
anon
Do namespaces have a different(somwhat specific) role in C++? What's wrong with the quoted text.
Prasoon Saurav
+4  A: 

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.

Ragster
+9  A: 

You can google 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.

Abhay
+2  A: 

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.

bta
+3  A: 

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

anon