如何在 C++ 中实现可插值接口
How to implement an interpolatable interface in C++
我正在尝试实现软件渲染器,该插值发生在顶点着色之后
以下是其声明
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 的插值方法 return 成为 Vertex 的实例?
编译器一直给我错误
您可以安全地将 class' 名称作为模板参数传递,但您遇到的任何错误都是由于函数签名不匹配造成的。
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;
如果您修复了三个错误,它应该会起作用:
?????????
应该是 Vertex
interpolate
应该 return Vertex
按值
interpolate
不应该是 const
(或者应该是 const
在基础 class 中)
我正在尝试实现软件渲染器,该插值发生在顶点着色之后
以下是其声明
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 的插值方法 return 成为 Vertex 的实例? 编译器一直给我错误
您可以安全地将 class' 名称作为模板参数传递,但您遇到的任何错误都是由于函数签名不匹配造成的。
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;
如果您修复了三个错误,它应该会起作用:
?????????
应该是Vertex
interpolate
应该 returnVertex
按值interpolate
不应该是const
(或者应该是const
在基础 class 中)