Hello everybody,
I was wondering if there is any way to store inherited classes in a vector object. I want to use only one vector, if possible, to store different classes that are inherited by a base class. I tryed the following code but it is lossing data that are stored in the inherited class but they are not declared in the base class.
The way to do this would be with a vector of base pointers. That way, you can use dynamic_cast to cast those pointers back as an inheri before calling the show() function. Here's a modified main().
int main(){
vector<base*> viktoras;
inheri* taksh = new inheri();
taksh->store(4);
viktoras.push_back(taksh);
cout << (dynamic_cast <inheri*> (viktoras[0]))->show();
return 0;
}
Above solution is correct, but if you are using it in some project, then take care of deleting the memory allocated through these new operator. Other wise it will cause memory leak.
// do something else
// call another subclass dependent method
if (dynamic_cast <inheri*> (viktoras[0])) {
(dynamic_cast <inheri*> (viktoras[0]))->store(4);
} else if (dynamic_cast <inheri2*> (viktoras[0])) {
(dynamic_cast <inheri2*> (viktoras[0]))->store(4);
}
return 0;
}
Where inheri2 is another subclass of "base".
As you can see, with every additional method and every additional subclass, this starts to get really messy. So in short, is there any way to make this cleaner???
Ok, I tryed this but i have a problem storing multiple instances of the same inherited class. The vector holds the place in memory a store the last inherited class, so I have an array full of pointers that point to the same position.
Is there any way to store the actual data in the vector?
Your code putting the taksh instances into the vector is fine, apart from the delete on line 25. This will make the pointer you have just put in the vector point to nothing because you are deleting it. The push_back() is only taking a copy of the pointer, not the instance it is pointing to. You also have a problem with the cout. I suspect this code has been modified a bit while you try to find the problem, but in case it isn't;
You are only displaying the first one in position zero. You need something like the following
1 2 3 4 5 6
for (int i = 0; i < viktoras.size(); i++)
{
cout << (dynamic_cast <inheri*> (viktoras[i]))->show();
}
Also don't forget to delete the pointers in the vector, after you have finished with them.
I just used the (viktoras[0]))->show(); because i wanted to test the vector if it keeps the first results.
What I want to do is save several instances of inherited classes.
For exaple, to save 2 inheri type in the vector and the same time to keep the ability of storing other type of inherited classes also.
If I don't delete the pointer then I can't create a completly new instance of the same class...
(Inherited and Derived is the same thing, right? if not, i mean derived classes...)
base * and inherited * are all stored in the vector!
Yes, I can see that both base and inherited are stored, but you are losing the first value (taksh->store(4);)... You don't have the inheri with the value of 4 any more.
I want to store uknown number of instances of the inheri class...
And the same time be able to store a base class also...
Okay, Okay...
From what I gather, lines 42 and 50 were introduces a while ago to show how to properly delete/remove items from the vector. Delete these two lines and you'll get you first item back.
This also leads me to believe that line 62 is not safe alone. Not sure.
The real question is how do you want to populate the vector.
Here is one way to achieve populating the vector based on keyboard input.
When you do a new or a malloc, you take memory off the heap. While that memory is allocated, you should always keep a pointer to the allocated region in scope. Once you have finished with it call delete or free. If you loose the pointer before it is released, that memory is completely lost for the duration of the programs execution, i.e. in software speak, it has leaked away; a memory leak.