3

Ok, so I was reading this book called 'A Tour of C++' by Bjarne Stroustrup and it had the class and namespace scopes defined as follows:

Class scope: A name is called a member name (or a class member name) if it is defined in a class, outside any function or enum class. Its scope extends from the opening { of its enclosing declaration to the end of that declaration.

Namespace scope: A name is called a namespace member name if it is defined in a namespace outside any function, , class, or enum class. Its scope extends from the point of declaration to the end of its namespace.

Quite frankly, I was unable to comprehend the difference. If someone could be kind enough to explain this in simpler terms or more detail for a beginner, it would be really appreciated.

And what exactly does 'namespace' mean? Is class scope as of a member variable of a class and namespace scope as of a global variable outside any class? Is my interpretation correct?

4
  • Also, what exactly does 'namespace' mean? Commented Mar 16, 2015 at 1:26
  • There is a global namespace, whose scope spans the entire file. Commented Mar 16, 2015 at 1:28
  • Ok, what exactly is a namespace again? Commented Mar 16, 2015 at 1:29
  • @Grendan google.co.nz/search?q=c%2B%2B+namespaces Commented Mar 16, 2015 at 2:17

3 Answers 3

1

what exactly does 'namespace' mean?

Say I write this:

namespace ns1
{
    class foo{}
}
namespace ns2
{
    class foo{}
}

Now even the name of both class is same the compiler would treat them as: ns1::foo and ns2::foo i.e. they are different class.

Class Scope and namespace scope:

namespace ns1{
    int a; // a has existence from here
    class foo{
        int b; // b has existence from here
    }          // b has existence upto here
}  //a has existence upto here
Sign up to request clarification or add additional context in comments.

3 Comments

Very interesting. What exactly is the use of it, besides what you just illustrated? Is that it? Like a namespace is used to have multiple classes or variables with the same name in the same source code file?
Mostly because conflicts can happen between libraries and application code, like I mention in my answer.
@Grendan yes namespaces are used to avoid naming conflicts. Say you write a sort function. But if you use #include <algorithm> and using namespace std; you have a naming conflict. Because sort is a standard function defined in <algorithm>
1

A class is a group of variables and procedures that are related to each other. They are tied together by the class. Example:

class Human {
public:
    int age;
    string name;
    void GrowOlder() { age += 1; }
};

All of the items inside that class definition are in the class scope. In order to use the class you may do something like this:

Human grendan;
grenden.name = "grenden";
grenden.GrowOlder();

The "name" and "GrowOlder" members of the "Human" class are used in this example. If you just write

name = "grenden"

that will be an error if you're currently outside of the "Human" class scope.

A problem happens when many libraries or applications try to interact with each other. Each one may have its own string library, its own vector math library, and so on, and so there may be duplicate class names. Namespaces are a solution to this problem. You can place your class in a namespace, in order to qualify it better.

namespace foo
{
    class Human {
    public:
        int age;
        string name;
        void GrowOlder() { age += 1; }
    };
};

Now the "Human" class is inside the "foo" namespace scope. If you're working from outside the namespace, "Human" doesn't mean anything anymore. Now in order to use the "Human" class the code looks like:

foo::Human grendan;
grenden.name = "grenden";
grenden.GrowOlder();

Notice the extra "foo::". This tells the compiler to look inside the "foo" namespace scope in order to find the "Human" class.

In general, you're inside a class scope if you're inside one of the procedures of that class.

You're inside a namespace scope if either you're inside a namespace {} block, or there is a "using namespace foo;" statement somewhere higher up in your code file.

Example:

void test()
{
    std::string example = "testest";
}

Or, with using namespace:

using namespace std;

void test()
{
    string example = "testest";
}

3 Comments

First off, thank you so much for the detailed explanation. So, when we say using namespace std, does it mean, we are referring to this namespace named std?
It means: "From now on in the current file, look in the namespace 'std' if you can't resolve some symbol." I've added an example for you to the answer.
Thank you so much Jorge. Really appreciate. :)
0

Differences between class/struct and namespace that I can think of:

  1. A class definition cannot be used multiple times. A namespace can be used multiple times to extend its contents.

    class A { int a; };
    class A { int b; };   // This is not allowed.
                          // You can extend the class by creating
                          // a sub-class and adding new members to
                          // the derived class but you cannot add
                          // members to a class by defining it again.
    
    
    namespace A { int a; }
    namespace A { int b; }   // Perfectly fine.
                             // b is an additional member of the namespace.
    
  2. You can create objects of a class. You cannot create objects of a namespace. A class has storage requirements, a namespace does not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.