Edge cases when replacing a type alias with inheritance

Viewed 52

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?

Edit: I've found one gotcha so far, you can't forward declare a type alias, so you might have had users work around that by re-typedefing in their headers: typedef Vector3 Point, which will break with the refactor.

0 Answers
Related