Renaming a class in C ++

I have a class that I would like to refer in my header file, which is a long chain of nested namespaces: MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass. I would like to use it under a different name, but not MyVeryLongNamedClass- something shorter and more useful, for example MyClass.

I could put using MySpaceA::MySpaceB::MySpaceC::MySpaceDin my header, but I do not want to import the entire namespace. I would rather have some kind of design like

using MyClass = MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass

I know this is possible with namespaces, but I can't get it to work with classes.

Many thanks for your help.

+5
source share
4 answers
typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass MyClass;

typedef:

template <typename T>
struct MyClass {
  typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T> type;
};

MyClass<T>::type MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T>.

+14

alias - typedef. ? ++ - . , , !

+2

,

template <typename T>
struct MyClass : public MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T> {
};

MyClass<int> foo;
MyClass<float> bar;
+2
using namespace MySpaceA::MySpaceB::MySpaceC::MySpaceD

"... MySpaceD"

using MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass

'..MyVeryLongNamedClass' .

"" typedef:

 #include <MyBigDeepNameSpaces.hh>

 namespace myPureNameSpace {
    typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass MySomething_t ;
 }

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
By: Herb Sutter; Andrei Alexandrescu
Publisher: Addison-Wesley Professional
Pub. Date: October 25, 2004
Print ISBN-10: 0-321-11358-6
Print ISBN-13: 978-0-321-11358-0

Chapter 57

(stop using the namespace! Put your cigarette in!)

0
source

All Articles