vendredi 18 février 2022

Edge cases when replacing a type alias with inheritance

I have a type Point that's simply an alias for Vector3: using Point = Vector3;

I'd like to be able to add functionality to Point, but no additional state. So I'd like to replace it with a class inheriting from Vector3:

class Point : public Vector3 {
  using Base = Vector3;
  using Base::Base;

  Point(const Base& base) : Base(base) {}
  Point(Base&&) : Base(std::move(base)) {}

  Point& operator=(const Base& base) { 
    Base::operator=(base); 
    return *this; 
  }

  Point& operator=(Base&& base) {
    Base::operator(std::move(base));
    return *this;
  }
};

This inherits the base class constructors and allows converting/moving values of Vector3 to Point since we could do that freely before.

The only thing that you could do before that you can't do with this method is freely casting pointers. Point* -> Vector3* still works, but Vector3* -> Point won't work implicitly anymore.

Are there any other corner cases when replacing a typedef in this fashion?

Aucun commentaire:

Enregistrer un commentaire