用专门版本覆盖多个继承的模板函数 [英] Overriding multiple inherited templated functions with specialized versions

查看:176
本文介绍了用专门版本覆盖多个继承的模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,首先是示例代码;这是我尝试传达我想要做的是什么,虽然它不编译:

Okay, sample code first; this is my attempt at communicating what it is that I'm trying to do, although it doesn't compile:

#include <iostream>

template <class T>
class Base
{
public:
	virtual void my_callback() = 0;
};

class Derived1
	: public Base<int>
	, public Base<float>
{
public:
	void my_callback<int>()
	{
		cout << "Int callback for Derived1.\n";
	}
	void my_callback<float>()
	{
		cout << "Float callback for Derived\n";
	}
};

class Derived2
	: public Base<int>
	, public Base<float>
{

public:
	void my_callback<int>()
	{
		cout << "Int callback for Derived2.\n";
	}
	void my_callback<float>()
	{
		cout << "Float callback for Derived2\n";
	}

};

int main()
{
	{
		Derived1 d;
		Base<int> * i_p = d;
		Base<float> * i_f = d;

		i_p->my_callback();
		i_f->my_callback();
	}
	{
		Derived2 d;
		Base<int> * i_p = d;
		Base<float> * i_f = d;

		i_p->my_callback();
		i_f->my_callback();
	}

	//Desired output:
	// Int callback for Derived1.
	// Float callback for Derived1
	// Int callback for Derived2.
	// Float callback for Derived2
	system("Pause");
}

所以,我想做的是做一个包装类继承自动将派生类连接到各种回调列表;它需要将一个特定的派生类的实例连接到列表,我想让用户有/ get来做回调函数作为派生类的一部分,你可以看到。

So, what I'm trying to do is to make a sort of wrapper class to inherit from that will automatically connect the derived class to various callback lists; it needs to connect a specific instance of the derived class to the list, and I want the "user" to have / get to make the callback functions as part of making the derived class, as you can see.

似乎这应该能够工作,虽然我可能需要使用不同的语法。

It seems like this should be able to work, although I may need to use a different syntax. If it can't work, do you have any suggestions?

推荐答案

是的,你可以让这项工作:

Yes, you can make this work:

#include <iostream>
using namespace std;

template <class T>
class Base
{
public:
  virtual void my_callback() = 0;
};

class Derived1 : public Base<int>, public Base<float>
{
public:
  void Base<int>::my_callback() {
    cout << "Int callback for Derived1.\n";
  }
  void Base<float>::my_callback() {
    cout << "Float callback for Derived\n";
  }
};

class Derived2 : public Base<int>, public Base<float>
{
public:
  void Base<int>::my_callback() {
    cout << "Int callback for Derived2.\n";
  }
  void Base<float>::my_callback() {
    cout << "Float callback for Derived2\n";
  }
};

int main()
{
  {
    Derived1 d;
    Base<int> * i_p = &d;
    Base<float> * i_f = &d;
    i_p->my_callback();
    i_f->my_callback();
  }
  {
    Derived2 d;
    Base<int> * i_p = &d;
    Base<float> * i_f = &d;
    i_p->my_callback();
    i_f->my_callback();
  }
}

输出:

Int callback for Derived1.
Float callback for Derived
Int callback for Derived2.
Float callback for Derived2

这篇关于用专门版本覆盖多个继承的模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆