简体   繁体   中英

C++ adding class to a namespace: why?

I'm a newbie in c++. Why, in Eclipse (configured with MinGW) and also in other threads, I noticed is used to add a class to a namespace?

I provide an example to show you my actual doubt:

#ifndef MODEL_MANGO_HPP_
#define MODEL_MANGO_HPP_

namespace std {

class Mango {
public:
    Mango();
    virtual ~Mango();
};

} /* namespace std */

#endif /* MODEL_MANGO_HPP_ */

EDIT : As shown in comments, it's completely forbidden to add classes to namespace std . Quoting @owacoder,

Namespaces are never closed, so you always have the ability to add class definitions to them. However, by the specification the std namespace is to be considered closed.

To provide you a complete view of the context, here is the default implementation of the Mango.cpp, that Eclipse has done for me:

#include "Mango.hpp"
namespace std {
Mango::Mango() {
    // TODO Auto-generated constructor stub
}
Mango::~Mango() {
    // TODO Auto-generated destructor stub
}
} /* namespace std */

So my question changes into: Why it's used "namespace std {...}" and when is a good practice to add classes to a namespace?

You have to understand the basics of what classes and namespaces are.

classes (along with structs, enums, and enum classes) are used to define user defined types in C++.

You create a class to represent a logical entity and encapsulate details, etc.

namespaces are a way to mark territories of code and qualifying unique names for variables.

if you just write a class in a file, it will be written in the "global namespace" and it is not considered good practice because you are "polluting the namespace".

instead, you should use namespaces to limit the scope where your variable names have meaning. this way, you are not exhausting the pool of sensible class and variable names quickly (how many times have you wanted to write a "Utility" class?)

namespace firstNamespace{
int x=2;
}

namespace secondNamespace{
int x=7;
}

int main () 
{
std::cout << firstNamespace::x << '\n';
std::cout << secondNamespace::x << '\n';
return 0;
}

in this case, you can see that we can "reuse" the variable name x in different Contexts by qualifying a namespace. inside the namespace blocks, we could have more declarations and definitions. including functions, classes, structs, etc.

take not that namespaces remain open and you can add to them later. for example you can have this:

namespace firstNamespace{
int x=2;
}

namespace secondNamespace{
int x=7;
}

namespace firstNamespace{
int y=11;
}

here, we added firstNamespace::y.

More importantly, you can observe that std is a namespace provided by C++ that contains a lot of useful variables, objects like cout which is of type std::ostream, functions and classeslike std::vector, std::ostream, etc.

so to go back to your question, the reason you want to wrap your class definitions in namespaces is to not pollute the global namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM