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

Sorry for being afk and replying once in a while :/ Parent probably has a copy constructor Parent(const Parent& p)

Parent p = Child(); calls it by implicitly casting Child& to const Parent&. That’s what happens.

I see that you must have meant this casting step, so indeed the constructor has no control over the rejigging.




I see what you mean, but let's go back to the orginal issue and why I think this implicit cast is insufficient as as definition of what happens here.

Say you have the obvious minimal implementations of

  struct Point ...
  struct Point3D : public Point ...
And then you do this:

  Point p3 = Point3D(1.1, 2.2, 3.3);
  Point p = p3;
On a 64 bit machine this results in sizeof(p) == 16 and sizeof(p3) == 24. So, at some point someone has to decide how to transform p3's 24 byte representation into p's 16 byte representation.

In C++, this happens simply by truncting p3, i.e copying the leading 16 bytes of p3 into p and throwing away the trailing 8 bytes (I'm not sure whether or not this is part of the standard).

This is not the only imaginable way in which this could possibly happen though, which is why I'm saying that the whole process is not sufficiently defined by stating that a reference to child is cast to a reference to parent.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: