对象的结构覆盖定义的方法? [英] Object's structure overriding defined methods?

查看:118
本文介绍了对象的结构覆盖定义的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 Object

class Object {
  public:
    Vector pos;
    float emittance;
    Vector diffuse;

    virtual float intersection(Ray&) {};
    virtual Vector getNormal(Vector&) {};
};

另一个继承它的类:

class Sphere: public Object {
  public:
    float radius;

    virtual float intersection(Ray &ray) {
      Vector distance;
      float b, c, d;

      distance = ray.origin - pos;
      b = distance.dot(ray.direction);
      c = distance.dot(distance) - radius*radius;
      d = b*b - c;

      cout << -b - sqrt(d);

      if (d > 0.0) {
        return -b - sqrt(d);
      } else {
        return false;
      }
    }

    virtual Vector getNormal(Vector position) {
      return (position - pos).norm();
    }
};

当我编译程序时,我期待它开始吐出吨和吨的文本行。但是由于某种原因,整个方法( intersection()方法)从来没有真正调用过!

When I compiled the program, I was expecting it to start spitting out tons and tons of lines of text. But for some reason, that whole method (the intersection() method) is never actually called at all!

为什么我的 intersection()函数从 Sphere 类不覆盖 Object class

Why is my intersection() function from the Sphere class not overriding the default on found in the Object class?

推荐答案

您没有将函数声明为 virtual 并确保方法签名匹配。更改为:

You did not declare the function as virtual and make sure that the method signature matches. Change it to:

class Object{    
    virtual float intersection(Ray) {};
    virtual Vector getNormal(Vector) {};
}

class Sphere: public Object {
    ...
    virtual float intersection(Ray ray) {
...

这篇关于对象的结构覆盖定义的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆