The &rect holds the start point of a larger memory area then the pointer Polygon represents. |
I think you're saying that a Polygon pointer should ALWAYS "represent" only a Polygon object, just a base Polygon, not ever something derived from Polygon.
That is incorrect.
The pointer Polygon does NOT "represent" a base Polygon object only. The pointer Polygon "represents" ANYTHING derived from a Polygon type.
A Polygon pointer should point at something that is a Polygon. Is a Polygon a Polygon? Yes. So that's fine. Is a Rectangle a Polygon? Yes. So that's fine. Is a Triangle a Polygon? Yes. So that's fine.
The original concern was "unknown territory will be overwritten". What unknown territory? What lump of memory that was unknown will have been overwritten?
The addresses are actually starting points to a memory area that is being specified by the pointer type. |
That's incorrect. The pointer does NOT specify how much memory the object being pointed to takes up. Take this very example. We have a Polygon pointer, pointing at a Rectangle. That's fine, because a Rectangle IS a Polygon. We could instead make that pointer point at a base Polygon object, which would also be fine. The pointer type does NOT tell you anything about how much memory the object it's pointing to occupies. It's got nothing to do with that.
Assuming that someone hasn't screwed with the type-safety, in this example code, when you see a Polygon pointer you know that whatever it is pointing to has the class function
set_values.. and that's ALL you know. You don't
know if it's a base Polygon, or a Rectangle, or a Triangle. The pointer doesn't know. This is how it works.
As an aside, analogies in programming are almost always unhelpful. Analogies about shoe sizes are completely irrelevant here. You seem to be suggesting that we're taking something big, and trying to fit it into a small space? We're not. At all. Anywhere. That idea doesn't apply to anything at all in this situation.
Is it possible for a bad programmer who thinks that a Polygon pointer will ONLY ever point to a base Polygon object to write bad code because the Polygon pointer is in fact pointing to a Rectangle? Yes. It's called "object slicing" and it's bad. Don't do it.