Skip to main content
Tweeted twitter.com/StackSoftEng/status/1445765837466464262
deleted 13 characters in body
Source Link
a a
  • 261
  • 2
  • 7

I have the following interface:

class IHittable
{
    virtual Intersections intersects(const Ray & ray) = 0
}

which will be implemented by various geometric objects(Spheres, Triangles etc). Now the class World will look like this:

class World
{
  public:
    Intersection intesects(const Ray & ray)
    {
      auto intersections = Intersections();
      for(auto object : objects)
      {
        auto xs = object.intersection();
        if(xs.size() > 0)
        {
          intersections.append(xs);
        }
      }
      return intersections;
   }
  private:
     std::vector<std::shared_ptr<IHittable>> objects;
}

My question is, should the class World also implement the IHittable interface? because its member looks the same and does exactly the same thing? How bad is it if I don't do that?

A second more generall question(subquestion) if this is allowed: When should I make a function free standing and when not, in my case the function intersects inside the World class could be easily made free standing just with one extra argument. I don't know how to think about such cases.

Am I going against the "Single Responsibility Principle" if Intersects is a member of World?

I have the following interface:

class IHittable
{
    virtual Intersections intersects(const Ray & ray) = 0
}

which will be implemented by various geometric objects(Spheres, Triangles etc). Now the class World will look like this:

class World
{
  public:
    Intersection intesects(const Ray & ray)
    {
      auto intersections = Intersections();
      for(auto object : objects)
      {
        auto xs = object.intersection();
        if(xs.size() > 0)
        {
          intersections.append(xs);
        }
      }
      return intersections;
   }
  private:
     std::vector<std::shared_ptr<IHittable>> objects;
}

My question is, should the class World also implement the IHittable interface? because its member looks the same and does exactly the same thing? How bad is it if I don't do that?

A second more generall question(subquestion) if this is allowed: When should I make a function free standing and when not, in my case the function intersects inside the World class could be easily made free standing just with one extra argument. I don't know how to think about such cases.

Am I going against the "Single Responsibility Principle" if Intersects is a member of World?

I have the following interface:

class IHittable
{
    virtual Intersections intersects(const Ray & ray) = 0
}

which will be implemented by various geometric objects(Spheres, Triangles etc). Now the class World will look like this:

class World
{
  public:
    Intersection intesects(const Ray & ray)
    {
      auto intersections = Intersections();
      for(auto object : objects)
      {
        auto xs = object.intersection();
        if(xs.size() > 0)
        {
          intersections.append(xs);
        }
      }
      return intersections;
   }
  private:
     std::vector<std::shared_ptr<IHittable>> objects;
}

My question is, should the class World also implement the IHittable interface? because its member looks the same and does exactly the same thing? How bad is it if I don't do that?

A second more generall question if this is allowed: When should I make a function free standing and when not, in my case the function intersects inside the World class could be easily made free standing just with one extra argument. I don't know how to think about such cases.

Am I going against the "Single Responsibility Principle" if Intersects is a member of World?

Source Link
a a
  • 261
  • 2
  • 7

Single Responsibility Principle, Inheritance

I have the following interface:

class IHittable
{
    virtual Intersections intersects(const Ray & ray) = 0
}

which will be implemented by various geometric objects(Spheres, Triangles etc). Now the class World will look like this:

class World
{
  public:
    Intersection intesects(const Ray & ray)
    {
      auto intersections = Intersections();
      for(auto object : objects)
      {
        auto xs = object.intersection();
        if(xs.size() > 0)
        {
          intersections.append(xs);
        }
      }
      return intersections;
   }
  private:
     std::vector<std::shared_ptr<IHittable>> objects;
}

My question is, should the class World also implement the IHittable interface? because its member looks the same and does exactly the same thing? How bad is it if I don't do that?

A second more generall question(subquestion) if this is allowed: When should I make a function free standing and when not, in my case the function intersects inside the World class could be easily made free standing just with one extra argument. I don't know how to think about such cases.

Am I going against the "Single Responsibility Principle" if Intersects is a member of World?