typedef 从纯抽象基类继承
编辑:发现 重复
我已将一些问题代码缩减为最简单的工作案例说明以下内容:我的纯抽象基类中的 typedef 没有被派生类继承。在下面的代码中,我想将 system_t
typedef 继承到 ConcreteTemplateMethod
中:
#include <iostream>
// pure abstract template-method
template <typename T> // T == Analyzer<U>
class TemplateMethod {
public:
typedef T system_t;
virtual void fn (const system_t& t) const = 0;
};
template <typename T>
class Analyzer {
public:
void TemplatedAlgorithm (const TemplateMethod< Analyzer <T> >& a) const {
printf ("Analyzer::TemplatedAlgorithm\n");
a.fn(*this); // run the template-method
}
void fn () const {
printf ("Analyzer::fn\n");
}
};
// concrete template-method
template <typename T>
class ConcreteTemplateMethod : public TemplateMethod < Analyzer<T> > {
public:
typedef Analyzer<T> system_t;
virtual void fn (const system_t& t) const {
printf ("ConcreteTemplateMethod::fn\n");
t.fn(); // perform Analyzer's fn
}
};
int main () {
Analyzer <double> a;
ConcreteTemplateMethod<double> dtm;
a.TemplatedAlgorithm(dtm);
return 0;
}
此代码按预期编译和运行。在 ConcreteTemplateMethod
中,以下内容是必需的,删除后会导致编译器错误:
typedef Analyzer<T> system_t;
请注意,system_t
类型已在基类中进行 typedef
编辑, 然而。为什么继承时必须包含另一个 typedef?
我意识到我可以通过使用 typename TemplateMethod
在派生的
,但这有点冗长,我想避免每次继承并需要使用相同的 ConcreteTemplateMethod
中限定 system_t
的类型名称。分析仪T >::system_t&时都必须重新
。有没有办法可以在基本 typedef
到基类>system_tTemplateMethod
中定义解决这个问题?
Edit: Found duplicate
I've whittled down some problem code to the simplest working case to illustrate the following: my typedef in a pure abstract base class is not being inherited by the derived class. In the code below I'd like to inherit the system_t
typedef into the ConcreteTemplateMethod
:
#include <iostream>
// pure abstract template-method
template <typename T> // T == Analyzer<U>
class TemplateMethod {
public:
typedef T system_t;
virtual void fn (const system_t& t) const = 0;
};
template <typename T>
class Analyzer {
public:
void TemplatedAlgorithm (const TemplateMethod< Analyzer <T> >& a) const {
printf ("Analyzer::TemplatedAlgorithm\n");
a.fn(*this); // run the template-method
}
void fn () const {
printf ("Analyzer::fn\n");
}
};
// concrete template-method
template <typename T>
class ConcreteTemplateMethod : public TemplateMethod < Analyzer<T> > {
public:
typedef Analyzer<T> system_t;
virtual void fn (const system_t& t) const {
printf ("ConcreteTemplateMethod::fn\n");
t.fn(); // perform Analyzer's fn
}
};
int main () {
Analyzer <double> a;
ConcreteTemplateMethod<double> dtm;
a.TemplatedAlgorithm(dtm);
return 0;
}
This code compiles and runs as expected. In the ConcreteTemplateMethod
the following is required, and when removed causes compiler errors:
typedef Analyzer<T> system_t;
Note that the system_t
type is already typedef
'ed in the base class, however. Why must I include another typedef when inheriting?
I realize that I can qualify the typename of system_t
in the derived ConcreteTemplateMethod
by using typename TemplateMethod< Analyzer<T> >::system_t&
, but that's a bit verbose, and I'd like to avoid having to re-typedef
to the base everytime I inherit and need to use that same system_t
. Is there a way around this that I can define in the base TemplateMethod
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该
“继承”typedef。 typedef 不会自动继承(如果编译器兼容)。
如果你查看堆栈溢出,会在某个地方重复这个问题。
you should do
to "inherit" typedef. typedef is not automatically inherited (if compiler is compliant).
if you look through stack overflow, there will be duplicate of this question somewhere.