I am doing a project which requires me to create arrays of non-homogeneous arrays.
Say the array is "arr".
Then arr[0] might be array of integers, arr[1] might be array of strings, etc.
For that purpose I am using pointers. I have a base class:
class base_class;
And then I have a template class
template<typename T>
class temp_class : public base_class{
private:
T* ptr;
.....
public:
void input(){
//do something}
};
Now I define the class arr.
class arr{
private:
base_class* ptr;
....
public:
void take_data(){
temp_class<int>* temp;
temp = static_cast<temp_class<int>*> (ptr)//OR (ptr+1)
temp->input();
}
}
But I get a segmentation fault(or something similar). I guess that the problem is due to the fact that two different types of pointers are pointing to the same object and the "input" function makes sense in only one of them.
How do I fix this? Is there any other way I can implement my idea, without these error prone pointers?