Re: Array Inheritance
* Al:
BS explained in his FAQ why vectors (or, more generally, arrays),
shouldn't support "conversion" to a version with a parent value type [1].
He also mentions that because of array/pointer decay, the above can
actually happen, currently, in an unsafe manner, with native arrays [2].
If I understand correctly, the reasons why this is unsafe are:
a) A base class may contain methods that a derived class doesn't.
Nope, but a derived class may have member functions or member variables
that aren't present in the base class.
The main point in C++ is that a derived class instance may contain more
than a base class instance.
Lay out two or more derived class instances after one another, and you
have have a problem. Say a base class instance is 10 bytes, and a
derived class instance is 50 bytes, and let b be a base class pointer
pointing to the first derived class instance. Now b[0] works nicely,
but b[1] points to byte 20, which is in the middle of the first derived
class instance, not at the start of the second one.
b) An object of type B1 might be added to an array of type B2,
indirectly, through a reference/pointer of array type A, where A is a
base class for both B1 and B2.
Nope.
But at a higher level there is the problem that a non-const container of
Derived is not logically a non-const container of Base.
IIRC, in Java this leads to a run-time error when you add an object of
type B1 (or for that matter A) to an array of B2. The corresponding
situation in C++ would be an array of pointers to B2 being treated as an
array of pointers to A. But the C++ compiler will just say no.
Therefore, I wonder if by removing these two, array inheritance would
work safely, and statically. That is to say:
a') Assume all methods are either virtual, or private.
b') Assume all "conversions" to a base array are implicitly const.
Given these changes, would it work?
No.
[1] http://www.research.att.com/~bs/bs_faq2.html#conversion
[2] http://www.research.att.com/~bs/bs_faq2.html#arrays
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]