Thread: Question about this method...

  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
    28,413
    Instead of having just one template parameter, have two template parameters (one more for the mapped type).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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, 09:22 PM