🔒 Locked
namespaces
Advertisement
Original Post
Lets say I integrate a 3rd party library into my project and one of the classes that is used has the same name as one of mine. Is there any way I can disable a namespace that is currently being used and switch to my own namespace? Or is there a way I can enable a namespace locally to just one class? It is too much of a pain to do MyNamespace::MyClass everytime I want to reference my class name.
for the typedef thing, i think it solved your problem.
didn't you asked for "is there a way I can enable a namespace locally to just one class"??
well typedef can do that.
EDIT: oopss, you said namespace. my bad.. :p. please ignore this post, sorry.
didn't you asked for "is there a way I can enable a namespace locally to just one class"??
well typedef can do that.
class A{ typedef int MyInt; MyInt a; };// this will cause compile error, because MyInt is not identified by class Bclass B{ MyInt b;};EDIT: oopss, you said namespace. my bad.. :p. please ignore this post, sorry.
// header1.hclass TheClassName { public: ...};// myheader.hnamespace MyStuff { class TheClassName { public: ... };}typedef MyStuff::TheClassName MyClassName;Of course, it's probably easier to just use search-and-replace in your own files to change the name of your class.
Quote:
Original post by EkEk
for the typedef thing, i think it solved your problem.
didn't you asked for "is there a way I can enable a namespace locally to just one class"??
well typedef can do that.class A{ typedef int MyInt; MyInt a; };// this will cause compile error, because MyInt is not identified by class Bclass B{ MyInt b;};
EDIT: oopss, you said namespace. my bad.. :p. please ignore this post, sorry.
Ok, thanks, I see what SiCrane was saying now. I know defines can’t localized and I assumed typedefs couldn’t either.
Quote:
Original post by ManaStone
Ok, thanks, I see what SiCrane was saying now. I know defines can’t localized and I assumed typedefs couldn’t either.
Naturally typedefs have scope. That's one of the reasons for using them instead of defines ;) And it's also fairly heavily depended upon by the standard library, and many template metaprogramming tricks.
Quote:
Original post by Anonymous Poster
likewise, you could also simply include "foreign" namespaces into a custom namespace, so that whatever library-namespaces always end up being wrapped in a pre-defined global namespace:
namespace mycustom_ns {
#include "library.hxx"
}
That's going to screw with linkage unless library is a C style API.
Quote:
Original post by EkEk
don't know if this is a good practice, but i used to do something like this
#define M MyNameSpace
M::MyClass
Given that there's a non #define equivilant, it's most definately not good practice.
That equivilant is:
namespace M = MyNameSpace;Quote:
Original post by ManaStone
It is too much of a pain to do MyNamespace::MyClass everytime I want to reference my class name.
Well, you can make it shorter (see above), or you can use a using statement. A using statement is generally considered very bad mojo in a header, however, considering that this affects all cide following the #include of that header, potentially leading to ambiguities - the entire reason for namespaces in the first place (avoiding name collisions/ambiguities).
There are two forms. To take the standard library typedef std::string :
#include <string>#include <map>using namespace std;string example = "foo"; //okay, maps to std::stringmap< int , int > example2; //also okay, maps to std::map<int,int>-------------------------------------------------------------------------------------#include <string>#include <map>using std::string;string example = "foo"; //okay, maps to std::stringmap< int , int > example2; //NOT okay, only using std::string. Use "using std::map;" to validate this
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement