重命名 C++ 中的类

发布于 2024-11-07 08:08:37 字数 458 浏览 3 评论 0原文

我想在头文件中引用一个类,该类位于一长串嵌套命名空间中:MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass。我想以不同的名称使用它,但不是 MyVeryLongNamedClass - 更短且更有用的名称,例如 MyClass

我可以将 using MySpaceA::MySpaceB::MySpaceC::MySpaceD 放在我的标头中,但我不想导入整个命名空间。我更喜欢某种结构,例如

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

我知道这对于命名空间是可能的,但我似乎无法做到这一点与班级一起工作。

非常感谢您的帮助。

I have a class that I would like to reference in my header file, which is in 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, like MyClass.

I could put using MySpaceA::MySpaceB::MySpaceC::MySpaceD in my header, but I do not want to import the whole namespace. I would prefer to have some kind of construction like

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

I know this is possible with name spaces, but I cannot seem to get it to work with classes.

Thank you very much for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不如归去 2024-11-14 08:08:38

using 不能用于给类起别名 - 为此你需要 typedef。您真的需要那些嵌套的命名空间吗? C++ 的命名空间功能从来没有打算成为一种架构机制 - 它只是为了防止名称冲突。如果您没有冲突(大多数情况下没有冲突),请不要使用它!

Using cannot be used to alias classes - for that you need typedef. And do you really need those nested namespaces? The namespace feature of C++ was never intended to be an architectural mechanism - it was simply there to prevent name clashes. If you don't have clashes, which mostly you don't, don't use it!

笑饮青盏花 2024-11-14 08:08:38

这似乎对我有用

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

现在你可以这样做

MyClass<int> foo;
MyClass<float> bar;

This seems to work for me

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

Now you can just do

MyClass<int> foo;
MyClass<float> bar;
情话墙 2024-11-14 08:08:38
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

第 57 章

(停止命名空间污染!熄掉你的香烟!)

using namespace MySpaceA::MySpaceB::MySpaceC::MySpaceD

Brings in the '...MySpaceD' namespace

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

Only brings in the '..MyVeryLongNamedClass' class into your namespace.

You can 'alias' it with a typedef:

 #include <MyBigDeepNameSpaces.hh>

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

Suggested Reading

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 namespace polution! Put out your cigarette!)

放我走吧 2024-11-14 08:08:37
typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass MyClass;

对于模板,您可以使用 ​​模板 typedef

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

现在您可以参考 < code>MyClass::type 而不是 MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass

typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass MyClass;

For templates, you could use a template typedef:

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

Now you can refer to MyClass<T>::type instead of MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文