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 <string, *pointers-to-objects*> 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 :)!

Really im not entirely sure why you want to do this, but one method available for this is polymorphism. But it is meant to provide a common functionality and hide the details of how / by whom the work is handled.

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

Well whilst that is possible, i consider it better practise when handling that kind of thing to do one of two things,

either create all object through a factory design pattern and have the factory assign all the pointers on object construction, or have an attach method to pass in your pointers.

The benefit of the this approach is that you can see clearly in each object whom it can talk to and keeps things clean.

Again thats all just my personal opinion.

One other alternative is to make a pointer manager class. pass this class by pointer or reference to all other objects. In the manager class impliment methods for all other objects to to both register thier own pointers and to get a pointer to any other object.

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.... :)

Thats an idea, but templates are used to work on object of the same type ( as far as i know) the idea there is that you write the code once and then it can be reused for any variable type but i dont think you can mix types inside a template.

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 :)

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;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.