[英]C++ HowTo: Non-template class inheriting from an abstract specialized template
I have a non-template abstract base class which is used in order to be able to have a reference to the base type, since unspecialized template types can't be used as method arguments. 我有一个非模板抽象基类,该基类用于能够对基类型进行引用,因为未专用的模板类型不能用作方法参数。
#ifndef RPCPP_ICALLBACKBASE_H
#define RPCPP_ICALLBACKBASE_H
#include <string>
namespace rpcpp
{
class ICallbackBase
{
public:
virtual ~ICallbackBase() {};
virtual void OnSuccess(void result) = 0;
virtual void OnError(std::string error) = 0;
};
}
#endif // RPCPP_ICALLBACKBASE_H
The abstract template class ICallback inherits from ICallback base, like so: 抽象模板类ICallback继承自ICallback基类,如下所示:
#ifndef RPCPP_ICALLBACK_H
#define RPCPP_ICALLBACK_H
#include "ICallbackBase.h"
namespace rpcpp
{
template <class T>
class ICallback : public ICallbackBase
{
public:
virtual ~ICallback() {};
virtual void OnSuccess(T result) = 0;
virtual void OnError(std::string error) = 0;
};
}
#endif // RPCPP_ICALLBACK_H
Finally, one can create a concrete type by inheriting from ICallback: 最后,可以通过继承ICallback来创建具体类型:
#ifndef RPCPP_SAMPLE_CALLBACK_H
#define RPCPP_SAMPLE_CALLBACK_H
#include "ICallback.h"
#include <iostream>
namespace rpcpp
{
class SampleCallback : public ICallback<double>
{
public:
~SampleCallback() {};
virtual void OnSuccess(double result)
{
std::cout << "Successfully executed a remote procedure, A + B = " << result << "\r\n\r\n";
}
virtual void OnError(std::string error)
{
std::cout << "Error while executing a remote procedure: " << error << "\r\n\r\n";
}
};
}
#endif // RPCPP_SAMPLE_CALLBACK_H
All of which compiles nice, however when i try to use this, like so: 所有这些都编译很好,但是当我尝试使用它时,就像这样:
rpcpp::SampleCallback sc;
sic.CalculateMean(15, 28, &sc); // Third argument of this method is expected to be ICallbackBase&.
It produces the following two errors: 它产生以下两个错误:
"cannot instantiate abstract class" in line #1. 第1行中的“无法实例化抽象类”。 "cannot convert parameter 3 from SampleCallback& to ICallbackBase&" in line #2
第2行中的“无法将参数3从SampleCallback&转换为ICallbackBase&”
What am I doing wrong? 我究竟做错了什么?
virtual void OnSuccess(void result) = 0;
从未定义。
you are not implementing every abstract method: 您没有实现每个抽象方法:
virtual void OnSuccess(void result) = 0;
virtual void OnSuccess(void result)= 0;
and what is the definition of CalculateMean? CalculateMean的解释是什么?
Here's how to fix the actual problem: 解决实际问题的方法如下:
Remove the parameter from OnSuccess: 从OnSuccess中删除参数:
virtual void OnSuccess()=0;
Add a constructor parameter to ICallback: 向ICallback添加构造函数参数:
template<class T> class ICallBack
{
public:
ICallBack(T &result) : result(result) { }
protected:
T &result;
};
Add new methods to allow non-template code to access T internal representation: 添加新方法以允许非模板代码访问T内部表示形式:
virtual void *Address() const=0;
virtual size_t Size() const=0;
virtual type_info Type() const=0;
Implement those methods in ICallBack<T>
: 在
ICallBack<T>
实现这些方法:
void *Address() const { return &result; }
size_t Size() const { return sizeof(T); }
type_info Type() const { return typeid(T); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.