如何解决子类继承基类的问题,同时基类是模板参数
#include <iostream>
using namespace std;
template <typename Child>
struct Base
{
void interface()
{
static_cast<Child*>(this)->implementation();
}
};
template<typename T,
template<class T> class Base
>
struct Derived : Base<Derived >
{
void implementation(T t=0)
{
t = 0;
cerr << "Derived implementation----" << t;
}
};
int main()
{
Derived<int,Base<Derived>> d; //Base class as a template parameter is must
d.interface(); // Prints "Derived implementation"
return 0;
}
我希望派生
类从模板参数base
类继承,同时希望base> base
类的实例取决于derived
类,派生
类有另一个模板参数t
,我尝试了多种方法,但无法解决。
基类作为模板参数必须
我已经简化了源代码,但是我描述的这些条件是必要的。
现在,我希望在我描述的这些情况下,我可以称呼interface()
成功地
知道有人知道问题在哪里以及如何解决?
#include <iostream>
using namespace std;
template <typename Child>
struct Base
{
void interface()
{
static_cast<Child*>(this)->implementation();
}
};
template<typename T,
template<class T> class Base
>
struct Derived : Base<Derived >
{
void implementation(T t=0)
{
t = 0;
cerr << "Derived implementation----" << t;
}
};
int main()
{
Derived<int,Base<Derived>> d; //Base class as a template parameter is must
d.interface(); // Prints "Derived implementation"
return 0;
}
I hope Derived
class inherits from a template parameter Base
class,meanwhile hope the instance of Base
class depend on Derived
class, the Derived
class have another template parameterT
,I have tried many ways but can't solve it .
Base class as a template parameter is must
I have simplified the source code, but these conditions I describe are necessary.
Now I hope that under these conditions I describe, I can call the interface()
sucessfully
Does anyone know where the problem is and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在线演示
Online Demo