Thread: custom loose coupling from string

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would like to echo anon and ask why do you want to do this in the first place.
    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

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I'll echo the same query and reiterate my question on the safety of casting a child method pointer to its parent. Is this safe? Seems to be breaking a lot of things I wouldn't have thought possible, to be honest.

  3. #18
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    The Purpouse is simply Play a puzzle game with C++
    Well this is the architecture of my wired application
    --------------------
    There is a Map like.
    buy => theBuyMethod()
    sell => theSellMethod()
    accept a LISTENERNAME as string.
    Now you have to call the method designated to the LISTENER
    e.g. If the user enters buy call theBuyMethod.

    I cant use a switch case here cause I want it to bind automatically.
    If I use a switch case I need to update the switch case each time I add a new member.

    am I able to make you understand.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you already have a map, why not just find the string in the map's keys? If it is found, call the function that the key maps to.

    You would need to update the map each time you add a new function, but this is the point of the map.
    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. #20
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    This is what currently going on.
    But its showing problems (which is solved using reinterpret_cast) during polymorphic Classes.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah yes, I see that now. It did sound very much like what twomers proposed.

    At the moment, one problem is that you have to map to the actual member functions themselves, so polymorphism is problematic. One possible solution that comes to mind is to map to a free function that takes an object by (const) reference and then calls the appropriate member function. Polymorphism will then work as intended. For example:
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    class Base
    {
    public:
        virtual void foo() const
        {
            std::cout << "Base::foo()" << std::endl;
        }
    };
    
    class Derived : public Base
    {
    public:
        virtual void foo() const
        {
            std::cout << "Derived::foo()" << std::endl;
        }
    };
    
    void foo(const Base& x)
    {
        x.foo();
    }
    
    typedef void (*MappedFunc)(const Base&);
    
    int main()
    {
        std::map<std::string,MappedFunc> functions;
        functions["foo"] = foo;
    
        Derived derived;
        std::map<std::string,MappedFunc>::iterator iter = functions.find("foo");
        if (iter != functions.end())
        {
            iter->second(derived);
        }
        else
        {
            std::cout << "No such function." << std::endl;
        }
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Custom String Parsing Function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2002, 11:07 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM