static_cast in C++

I was working on Circle class that is a child of Shape class and I have some tests that I have to pass that requires me to do some casting

    class Shape {
        friend bool operator == (const Shape& lhs, const Shape& rhs) {
            return (lhs._x == rhs._x) && (lhs._y == rhs._y);}
    
        friend istream& operator >> (istream& lhs, Shape& rhs) {
            return lhs >> rhs._x >> rhs._y;}
    
        friend ostream& operator << (ostream& lhs, const Shape& rhs) {
            return lhs << rhs._x << " " << rhs._y;}
          ...


 class Circle: public Shape {
            friend bool operator == (const Circle& lhs, const Circle& rhs) {
                return (lhs._radius == rhs. _radius) && static_cast<Shape>(lhs) == rhs;} // this line
              ...

How it's different if I did it this way? What's better?

   class Circle: public Shape {
                friend bool operator == (const Circle& lhs, const Circle& rhs) {
                    return (lhs._radius == rhs. _radius) && static_cast<const Shape&>(lhs) == rhs;}


Read more here: https://stackoverflow.com/questions/64900786/static-cast-in-c

Content Attribution

This content was originally published by Nour at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: