Reputation: 69
I'm trying to store various different types of data in an array or vector. So far I'm doing this by using a base class that will be stored in the vector as pointers to each object and then type casting to get the data back. This works great for int, but any other type of data throws a access violation exception.
Sorry if my explanation isn't very good, here is my code with comments that I hope will help:
//Base class
class MenuProperty
{
private:
std::string Name;
public:
MenuProperty(std::string Name) : Name(Name) {};
~MenuProperty() {};
std::string GetName();
};
//Typed class used to store data
template<class T>
class TMenuProperty : public MenuProperty
{
private:
T Data;
public:
TMenuProperty(std::string Name, T Data) : MenuProperty(Name), Data(Data) {};
T GetData()
{
return this->Data;
}
};
//Class with no type and data pointer to retrieve data
class cpMenuProperty : public MenuProperty
{
private:
VOID* Data;
public:
cpMenuProperty(std::string Name) : MenuProperty(Name) {};
VOID* GetPointer()
{
return this->Data;
}
};
Hope that makes some semblance of sense, here is my test code:
int main()
{
TMenuProperty<double> fP("Test2", 33.7354); //Create instance of property
MenuProperty* fMP = &fP; //Make a pointer to the object
cpMenuProperty* Test; //Make a pointer to the retrieving
//object
std::vector<MenuProperty*> Vec;
std::vector<MenuProperty*>::iterator it;
Vec.push_back(fMP);
it = Vec.begin();
Test = static_cast<cpMenuProperty*>(*it); //Cast the first object in the list
//list to the same type as the
//retrieveing object
double Data = *(double*)Test->GetPointer(); //Dereference and access, this is
//where the exception is thrown
std::cout << Data;
int Ret;
std::cin >> Ret;
}
I'm probably making some monumental error here, but thank you for taking the time to read it thus far :) Any help is appreciated, and constructive criticism too!
Upvotes: 0
Views: 174
Reputation: 19642
You're initializing a TMenuProperty object on the stack, which you're then casting to a cpMenuProperty. There is never any memory allocated for the void* Data in the cpMenuProperty. There is no relationship between TMenuProperty and cpMenuProperty, except that they're derived from the same class. This design is never going to work.
Upvotes: 2
Reputation: 2355
#include<iostream>
#include<vector>
#include<iterator>
#include<memory>
class base {
public:
virtual void foo(){
std::cout << "in base" << std::endl;
}
};
class derived : public base {
public:
virtual void foo(){
std::cout << "in derived" << std::endl;
}
};
int main()
{
std::vector<std::unique_ptr<base>> vec;
vec.emplace_back(new derived);
static_cast<derived*>(vec[0].get())->foo();
return 0;
}
classic example, using modern practices.
Upvotes: 0