Hi,
Suppose i have a class A and which has a subclass B is it possible to create an array which elements are objects of either class A or B?
If so, how can this be done?
Thanks..
Hi,
Suppose i have a class A and which has a subclass B is it possible to create an array which elements are objects of either class A or B?
If so, how can this be done?
Thanks..
If by subclass you mean B derives from A, then yes. Your array can be an array of pointers to A.
An array of pointers to a base class can store derived objects.
Note that you can also assign derived objects to an array of A. It will not be flagged as an error. But on this case, since the array is not storing pointers to the base class, but the base class itself, if you assign to one of its elements a derived object, only the base part of that object will be stored. It's called slicing...
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Thanks!
But now it seems that i have a problem with the array's elements' initialization..
Can you show the code?
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Sorry, it's quite large.
I'm just confused about the way i should call the constructor for each element after the array declaration.
If you could show me a small example i would really be grateful
> But now it seems that i have a problem with the array's elements' initialization..
Such an array would be dynamically sized with operator new or built out of existing instances. You could try to hack it I suppose, but in it's most glaring form, you end up taking the address of a temporary.
For instance, this is very wrong:
Let foo be a class whose constructor takes an integer, and bar a derivative class whose constructer has an additional char parameter.
Code:foo *array[] = { &foo(123), &bar(234, 'a'), &bar(567, 'b'), &foo(0) };
>> I'm just confused about the way i should call the constructor for each element after the array declaration.
You would use new to allocate space for the objects and store the pointer in the array. You would then have to remember to delete the pointers when you are done with them. A boost::ptr_vector is probably a better solution than an array because it handles the deletes for you.
You also have to make sure that class A has a virtual destructor.
I understand. But the problem is that i do not know how to store the pointer in the array.
It seems that i can't just use the assignment operator "="..
Show the code where you attempt to use the assignment operator, along with the declaration of the array.
Ok, i tried the following but nothing worked:
Code:class_a *table=new class_a[5]; class_a *node=new class_a("Jack"); table[0]=node;I really appreciate your concern..Code:vector <class_a*> vec(5); class_a *node=new class_a("Jack"); vec.push_back(node);
Thanks.
try this
Code:class_a **table=new class_a*[5]; class_a *node=new class_a("Jack"); table[0]=node;
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Didn't work either, i got the same errors:
expected constructor, destructor or type conversion before '=' token
expected ',' or ';' before '=' token
This Is working:
So do you have a class definition?Code:#include <iostream> class class_a { int x; }; int main() { class_a **table=new class_a*[5]; class_a *node=new class_a(); table[0]=node; }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Yeap, ok what you say is true indeed. I was just trying to add the pointer to the array outside a function (outside main to be honest). Thank you..
Could you possibly explain to me why
is needed? I thought that an array of pointers would be ok..Code:class_a **table=new class_a*[5];
it IS an array of pointers
new class_a*[5]; asks to allocate memory for 5 elements of the type
class_a*
class_a* - IS pointer to class
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler