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

ein*_*ica 2 c++ inheritance idioms idiomatic self-reference

假设我有一个

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> { ... }
Run Code Online (Sandbox Code Playgroud)

现在,A类的定义,和/或它的方法,我需要参考两个超类(例如,要在超成员访问,或者在它等定义的类型),但我想避免重复超的名字.目前,我正在做的是:

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>;
    ...
 }
Run Code Online (Sandbox Code Playgroud)

显然,这有效,并将重复次数减少到2; 但如果可能的话,我宁愿避免这种重复.有合理的方法吗?

笔记:

  • "合理",例如没有宏,除非有非常好的理由.

Jar*_*d42 6

在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>;
}
Run Code Online (Sandbox Code Playgroud)

然后

template<typename T>
class A : detail::parentA1<T>, detail::parentA2<T>
{
};
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,最好的方法是不污染范围 (4认同)