Question about this method...

This is a discussion on Question about this method... within the C++ Programming forums, part of the General Programming Boards category; I have a class called SmartMap. (how smart really depends I guess) The way I originally made it was to ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Question about this method...

    I have a class called SmartMap. (how smart really depends I guess)

    The way I originally made it was to use the type as the map element. This worked, but I need a different map for every type. So, I decided to make the map element a void* instead of the type itself, and just allow casting for the type.

    This is the class so far. I know it sucks. That is why its here.
    Code:
    template<class key>
    class SmartMap
    {
    public:
        SmartMap(HWND hwnd = 0):Hwnd(hwnd)
        {};
        void* Push(const key& new_key, void* new_val = 0)
        {
            std::map<key, void*>::iterator finder = PtrMap.begin();
            size_t loc = 0;
            for (/**/; finder != PtrMap.end(); ++finder )
            {
                if ( (*finder).first == new_key )
                {
                    std::stringstream ss1;
                    ss1 << "Key Found: " << new_key << "\nKey Location: " << loc\
                       << "\n\nCannot add new element. Key already exists.";
                    MessageBoxA(Hwnd,ss1.str().c_str(),"SmartMap<Key,Type> Error.",MB_OK);
                    return 0;
                }
                ++loc;
            }
    
            if ( new_val == 0 )
            {
                PtrMap[new_key] = new_val;
                return PtrMap[new_key];
            }
            else
            {
                PtrMap[new_key] = new void*(new_val);
                return PtrMap[new_key];
            }
            return 0;
        };
    private:
        //The actual container
        std::map<key, void*> PtrMap;
    
        //The window for the messagebox calls
        HWND Hwnd;
    
        //Disable copying
        SmartMap(const SmartMap&);
        const SmartMap& operator=(const SmartMap&);
    };
    Here is the only implementation I could get to work. Which is really ugly.
    Code:
        SmartMap<char> test;
        int* t = (int*)test.Push('a',(void*)1001);
        std::cout << *t << "\n\n";
    I am guessing that this is the only way to do this, but I am really not sure. Looking for input. Any help and I will be greatly appreciative.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Instead of having just one template parameter, have two template parameters (one more for the mapped type).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Yea, that is what I had before. That restricts the map to containing only one type of data. I am looking more for a map that contain different data types. Mainly to simplify my script language.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    I am looking more for a map that contain different data types. Mainly to simplify my script language.
    Are you sure you want the types to be completely unrelated? If so, perhaps boost::any will fit the bill. Or perhaps these types should fall under the same class hierarchy, in which case a map of std::tr1::shared_ptr<Base> or a boost::ptr_map may suffice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nothing is ever simplified by void pointers.

    If you want the map to store different data types, then write the map to store a single data type and have that type be able to contain different kinds of data. Like a std::map of boost::any or boost::variant.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Okay, I will look into boost::any. Thank you for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Method question...
    By keira in forum C++ Programming
    Replies: 14
    Last Post: 05-23-2008, 07:56 PM
  2. Callback function as class method
    By schifers in forum Windows Programming
    Replies: 39
    Last Post: 05-19-2008, 03:02 PM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  4. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21