c++ - 如何在 C++ 中实现可插值接口(interface)

标签 c++ templates inheritance interface

我正在尝试实现软件渲染器,在顶点着色之后进行插值

下面是它的声明

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/

相关文章:

c++ - 如何使用模板和继承来更正和改进此 C++ 代码?

java - 如何更改二维数组中对象的位置

c++ - 将包含指针的数据插入 vector

c++ - 对 std::vector 元素的赋值是线程安全的吗?

c++ - 如何使用 XCB 将按键事件发送到应用程序?

c++ - 查看编译器扩展代码 - C++

templates - 未知类型名称 ‘vector’

C++为什么不调用复制c'tor

python-2.7 - jinja2 忽略最后一行?

关于复制构造函数的 C++ OOP 程序问题?遗产