Difference between classes and namespaces?

49,029

Solution 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.

Solution 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.

Solution 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.

Solution 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.

Share:
49,029
TimothyTech
Author by

TimothyTech

Updated on July 09, 2022

Comments

  • TimothyTech
    TimothyTech almost 2 years

    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. Anyway, 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.

  • Admin
    Admin almost 14 years
    I don't really see how the quoted text relates to c++.
  • Prasoon Saurav
    Prasoon Saurav almost 14 years
    Do namespaces have a different(somwhat specific) role in C++? What's wrong with the quoted text.
  • Mike Seymour
    Mike Seymour almost 14 years
    +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.
  • PythonJin
    PythonJin over 9 years
    I found Scott Meyers post on How Non-Member Functions Improve Encapsulation to be quite helpful when learning about when to use namepsaces.
  • Chen Peleg
    Chen Peleg about 2 years
    The Edit part "languages like Java and C# regularly use classes as if they were namespaces" is very true, and realy helps explain things, especially if like me you are coming from OOP languages.