List to store pointers to diferent objects
Hi everyone,
I'm not even sure if this is possible, but I seem to be struggling at the minute with it so here goes...
I have a main.cpp where severeal different objects of different classes are created, such as say Log, ConfigManager, XMLReader and so on - what I'm trying to do is someone create a map say, that can hold the pointers too all these objects in one so later on I could for example look up an object and reuse it
like
Log* myLog = new Log (...);
map ObjectList;
ObjectList.insert ("Logger", myLog);
then later on if I wanted another object to use the Log, I could do something like
Log* newLogPointer = ObjectList.find ("Log");
Is it even possible to have something like this? I've tried experimenting with void* pointers but having no luck, any help would be much appreciated :)!
SCass2010
Junior Poster in Training
78 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
It's jsut out of interest really as I'm trying to learn more C++ - alot of my objects I'm having to pass in alot of pointers to say Log objects, config objects and so on, I was wondering if maybe there was a way of passing maybe a list of some sort of all these objects then calling/assigning them to the other objects :)
Have to take a peak at polymorphism too see if it could be done, like I said it's nothing major just trying it out of interest :P
SCass2010
Junior Poster in Training
78 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Yeah I was thinking that, jsut make a method to store and retrieve each type of class, but then if I needed to add a new type of class then it would mean adding more methods, maybe a template class, something like
template <class T>
T getObject (string name, T type)
{
ObjectMapIterator i = ObjectMap.find (name);
... then if found
T* returnType = dynamic_cast <T> (i);
return returnType;
}
Have too take a look at the factory desgn method, always meant to look at design patterns but get distracted too easily with mad ideas.... :)
SCass2010
Junior Poster in Training
78 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Hmmm... the template idea seems to have half worked - I stored the object as a void* in the map, it goes in ok, but when I get the object back out the address of the original object and the returned value are different :S and when I try to call a function frmo the returned object (the one that was stored as void*) the program just crashes... gonna need the thinking cap for definiate now :)
SCass2010
Junior Poster in Training
78 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Well, last update - but although the addresses are different, they all return the same name and log to the same file so they must be right :S
If anyone is interested, I sort of cheated to get around the problem... Made a class called "Module", that simply stores a module name e.g. "Log", then each of my other objects like Log, Config etc. all inherit from "Module" - so I have a map of Module classes and just dynamically cast depending on the type passed, prob not the best solution but it's a start anyways :)
class ObjectManager
{
public:
ObjectManager();
virtual ~ObjectManager();
void add (std::string, Module* obj);
bool find (std::string);
template <class T>
T* get (std::string key)
{
std::multimap <std::string, Module*>::iterator i;
i = mMap.find (key);
std::cout << "Address of module* " << i->second << std::endl;
T* obj = dynamic_cast <T*> (i->second);
std::cout << "Address of module* after dynamic cast " << obj << std::endl;
return obj;
}
protected:
private:
std::multimap <std::string, Module*> mMap;
};
SCass2010
Junior Poster in Training
78 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0