假设我有一个
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/