Hacker News new | past | comments | ask | show | jobs | submit login

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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: