J
Jack
Hi,
Is there a general solution for the following problem:
I have an array of instances of class B. Class B is publicly derived from
class A. Then I have a class named Buffer that generally takes care of
allocating and deallocating arrays. Class Buffer knows only that the objects
contained in its internal array are derived from class A but not that they
are exactly of type B, so Buffer has a member variable of type A* that
points at the beginning of that array. I know that indexing the elements
will not work with this mechanism, so I should rather have one more level of
indirection, that is, an array of elements of type A* that point to each B
object. But I think (please correct me if I am wrong) that the need to
allocate each object B separately slows down the operation, so I would like
to use new[] once instead of new multiple times. Okay, I can use first new
B[...] and then set one A* element in the array to point at each instance of
B. But is that sensible? Is there another way? Will delete[] clean up the
memory correctly if the pointer type given to it does not exactly match the
type of the elements contained in the array? (I mean, if the array of
objects B is deleted in the destructor of Buffer that knows only that the
elements are derived from class A)
I don't want to use virtual functions in class Buffer, because they cause
overhead in speed. I don't want to templatize it either, because it should
be enough for it to know that the objects contained in its internal array
are derived from class A. But still it should be able to index the elements
and destroy them correctly. What is the best way to do it?
Thank you very much for your help in advance,
Jack
Is there a general solution for the following problem:
I have an array of instances of class B. Class B is publicly derived from
class A. Then I have a class named Buffer that generally takes care of
allocating and deallocating arrays. Class Buffer knows only that the objects
contained in its internal array are derived from class A but not that they
are exactly of type B, so Buffer has a member variable of type A* that
points at the beginning of that array. I know that indexing the elements
will not work with this mechanism, so I should rather have one more level of
indirection, that is, an array of elements of type A* that point to each B
object. But I think (please correct me if I am wrong) that the need to
allocate each object B separately slows down the operation, so I would like
to use new[] once instead of new multiple times. Okay, I can use first new
B[...] and then set one A* element in the array to point at each instance of
B. But is that sensible? Is there another way? Will delete[] clean up the
memory correctly if the pointer type given to it does not exactly match the
type of the elements contained in the array? (I mean, if the array of
objects B is deleted in the destructor of Buffer that knows only that the
elements are derived from class A)
I don't want to use virtual functions in class Buffer, because they cause
overhead in speed. I don't want to templatize it either, because it should
be enough for it to know that the objects contained in its internal array
are derived from class A. But still it should be able to index the elements
and destroy them correctly. What is the best way to do it?
Thank you very much for your help in advance,
Jack