custom loose coupling from string

This is a discussion on custom loose coupling from string within the C++ Programming forums, part of the General Programming Boards category; I would like to echo anon and ask why do you want to do this in the first place....

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

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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
    20,974
    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.
    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. #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
    20,974
    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;
        }
    }
    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

Page 2 of 2 FirstFirst 12
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, 03: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

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