在另一个模板基类中实现抽象基类函数 [英] implementing an abstract base class function in another template base class
问题描述
我有一个Visual Studio 2008 C ++项目,其中我有一个类继承自一个抽象基类和另一个模板类,实现抽象基类中的一个函数。例如:
I have a Visual Studio 2008 C++ project where I have a class that inherits from an abstract base class and another template class that implements a function in the abstract base class. For example:
class Foo;
struct Buzz
{
virtual ~Buzz() {};
virtual void Buzz_Do( Foo* ) = 0;
};
class Base
{
public:
virtual ~Base() {};
virtual void Base_Do( Buzz* ) = 0;
};
template< class T >
class Bar
{
public:
virtual void Base_Do( Buzz* v )
{
v->Buzz_Do( static_cast< T* >( this ) );
};
};
class Foo : public Base, public Bar< Foo >
{
};
int _tmain(int argc, _TCHAR* argv[])
{
Foo c;
return 0;
}
$ b <
Unfortunately, this yields the compiler error:
1>MyApp.cpp(39) : error C2259: 'Foo' : cannot instantiate abstract class
1> due to following members:
1> 'void Base::Base_Do(Buzz *)' : is abstract
1> MyApp.cpp(17) : see declaration of 'Base::Base_Do'
a public using Bar < Foo> :: Base_Do; 到
class Foo
,但是没有帮助。
I've tried adding a public using Bar< Foo >::Base_Do;
to class Foo
, but that did not help.
是否有办法使这个工作,或者我需要在 Foo
中实现一个具体的 Base_Do()
并不使用 Bar<>
?
Is there a way to make this work or will I need to put a concrete Base_Do()
implementation in Foo
and not use Bar<>
?
谢谢,
PaulH
Thanks, PaulH
推荐答案
如果可能,让 Bar<>
派生自Base,
和Foo只有 Bar< T>
。
If possible let the Bar<>
derive from Base,
and Foo derive only from Bar<T>
.
这篇关于在另一个模板基类中实现抽象基类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!