c++ - 从派生类引用基类的更好习惯用法?

标签 c++ inheritance self-reference idioms

假设我有一个

template <typename T>
class A : 
    class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>,
    anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T> { ... }

现在,在类 A 的定义和/或其方法中,我需要引用两个父类(super class)(例如,访问父类(super class)中的成员或其中定义的类型等)。但是,我想避免重复父类(super class)名称。目前,我正在做的是:

template<typename T>
class A : 
    class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>,
    anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T> 
{
    using parent1 = class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>;
    using parent2 = anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T>;
    ...
 }

这很明显有效,并将重复次数减少到 2 次;但如果可能的话,我宁愿避免这种重复。有没有合理的方法来做到这一点?

注意事项:

  • “合理”,例如没有宏,除非有非常非常好的理由。

最佳答案

在A之前,你可以做

namespace detail
{
    template <typename T>
    using parentA1 = class_with_long_name<T,
                                          and_many_other,
                                          template_arguments,
                                          oh_my_thats_long>;
    template <typename T>
    using parentA2 = anotherclass_with_long_name<and_many_other,
                                                 template_arguments_that,
                                                 are_also_annoying,
                                                 including_also,
                                                 T>;
}

然后

template<typename T>
class A : detail::parentA1<T>, detail::parentA2<T>
{
};

关于c++ - 从派生类引用基类的更好习惯用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846812/

相关文章:

java - 异常: "No default Constructor provided" exception subclassing ParseObject

java - java中的自引用类和类加载器

带有宏的类中的 C++ 自动 shared_ptr 类型

c++ - 如何在不导入表的情况下编译C++ Windows exe

java - Java中C是A的子类时 "C c = new C()"和 "A c = new C()"的区别

C++ 类继承顺序

c++ - C++ 中以 Listclass-parent 作为成员的 Itemclass

entity-framework-4 - 具有多对多自引用关系的 EF Code First

使用智能指针的 C++ 链表实现 - 'head' 未在此范围内声明

c++ - C 中的链表