News for hackers. Stuff that matters.
Re:Inheritance Often Doesn't Make Sense by fallingfrog on Saturday March 17, 2018 @08:30AM (#16606667)
I think I get what you're saying here. On some level, an object method is a function which takes an object of a particular type and returns a new object of the same type.

So if you have a function like

Rectangle GetRectangleWithWidth(Rectangle, double) {...}

Which is equivalent to a setter method like

Rectangle::SetWidth(double) {...}

You can do:

Rectangle r, r';

r' = GetRectangleWithWidth(r,2.0);

But in that case doing something like

Square s, s';

s' = GetRectangleWithWidth(s, 2.0);

makes no sense, and it's pretty clear why. The return value is wrong. You can use a subclass as an argument to a function, but you can't return a superclass and just assign it to a subclass. And if you have some function

void Operate(Rectangle& r) { r = GetRectangleWithWidth(r, 2.0); }

then passing in a square should be a syntax error and not compile. Although, I'm not sure what C++ would actually do in this case.

There are no comments on this comment yet.