我正在尝试实现软件渲染器,在顶点着色之后进行插值
下面是它的声明
template <class T>
class Interpolatable
{
// The function calculates an interpolated value
// along the fraction t between 0.0 and 1.0.
// When t = 1.0, endValue is returned.
virtual T interpolate(const T &endValue, float t)=0;
};
struct Vertex: public Interpolatable<?????????>
{
float x, y, z;
Vertex()=default;
Vertex(float, float, float);
virtual Vertex &interpolate(const Vertex &endValue, float t) const;
};
是否可以让 Vertex 的插值方法返回 Vertex 的实例? 编译器一直给我错误
最佳答案
您可以安全地将类名作为模板参数传递,但您遇到的任何错误都是由于函数签名不匹配造成的。
struct Vertex: public Interpolatable<Vertex>
virtual T interpolate(const T &endValue, float t)=0;
virtual Vertex &interpolate(const Vertex &endValue, float t) const;
// ^reference ^declared const
看来你的签名应该是:
virtual T interpolate(const T &endValue, float t) const =0;
virtual Vertex interpolate(const Vertex &endValue, float t) const;
关于c++ - 如何在 C++ 中实现可插值接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28960115/