M
Michael Grünewald
Dear newsgroup,
I have a basis class A and derivatives B1…Bn and would like to build an
array containing various Bi's, and was unable to do so. Your help would
be much appreciated!
Ideally A would be a virtual (non instantiable) class, but this
constraint is not realizable if we want to declare an array of objects
of type A.
In my attempt, I defined a `=` operator for objects of type A
and declared an array
A T[n];
and initialised it like this
T[0] = B0();
…
Which (of course?) ended up with T containing n copies of the implicit
object of type A underlying Bi, but nothing like Bi.
Am I doing here something wrong or pointers are the only way to obtain
heterogeneous arrays?
Is an allocation à la C
A T[n] = { B0(), … };
allowed in C++ ?
I would also be very interested in literature references on this topic
or similar topics, I am very new to C++ (but skilled in other languages,
some OO, some not) and do not know yet about all these good books…
Thank you very much for your help!
I have a basis class A and derivatives B1…Bn and would like to build an
array containing various Bi's, and was unable to do so. Your help would
be much appreciated!
Ideally A would be a virtual (non instantiable) class, but this
constraint is not realizable if we want to declare an array of objects
of type A.
In my attempt, I defined a `=` operator for objects of type A
and declared an array
A T[n];
and initialised it like this
T[0] = B0();
…
Which (of course?) ended up with T containing n copies of the implicit
object of type A underlying Bi, but nothing like Bi.
Am I doing here something wrong or pointers are the only way to obtain
heterogeneous arrays?
Is an allocation à la C
A T[n] = { B0(), … };
allowed in C++ ?
I would also be very interested in literature references on this topic
or similar topics, I am very new to C++ (but skilled in other languages,
some OO, some not) and do not know yet about all these good books…
Thank you very much for your help!