怎么定义纯虚类内部的函数模板啊?

qugjie 2015-10-14 11:59:34
class KiteIOClient
{
public:
virtual ~KiteIOClient() = 0;
virtual void send(int cmdType, const protobuf::MessageLite& message) = 0;
virtual template<typename T> shared_ptr<T> sendAndGet(int cmdType, const protobuf::MessageLite &message) = 0;
virtual void registerListener(remotingListener listener) = 0;
virtual void start() throw (std::exception) = 0;
virtual bool reconnect() = 0;
virtual bool isDead() =0;
virtual void close() = 0;
virtual const string &getServerUrl() = 0;
virtual std::set<string>& getAcceptedTopics() = 0;
virtual bool handshake() = 0;
};


编译报错:
remoting/client/KiteIOClient.h:15:13: 错误:expected unqualified-id before ‘template’
virtual template<typename T> shared_ptr<T> sendAndGet(int cmdType, const protobuf::MessageLite &message) = 0;

...全文
197 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2015-10-14
  • 打赏
  • 举报
回复
函数模板不能是虚函数:

1、当模板类的某个成员函数被调用时,模板类被实例化。
2、在实例化这个模板类时,需要创建vertual table。
3、在模板类被实例化完成之前不能确定函数模板(包括虚函数模板,加入支持的话)会被实例化多少个。
4、普通成员函数模板无所谓,什么时候需要什么时候就给你实例化,编译器不用知道到底需要实例化多少个,虚函数的个数必须知道,否则这个类就无法被实例化(因为要创建virtual table)。因此,目前不支持虚函数模板。
www_adintr_com 2015-10-14
  • 打赏
  • 举报
回复
模板函数不能是虚函数

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧