Git Product home page Git Product logo

Comments (2)

dhbaird avatar dhbaird commented on August 10, 2024

Hi wezside! It is possible to bind a C++ method to a class instance and use it as a callback by using a "functor". No need to rewrite the dispatch signature, just need to bind the method to a class instance, for example,

class Functor {
    MyClass * instance;
  public:
    Functor(MyClass * instance) : instance(instance) {
    }
    void operator()(const std::string& message) {
        instance->memberFunction(message);
    }
};

int main() {
    // ...
    MyClass myClass;
    Functor functor(&myClass); // <-- a pre-C++11 way to bind instance+method
    webSocket->dispatch(functor); // <-- no need to change signature
    // ...
}

It is nicer if you can use a library that provides this binding capability for you though. Some libraries are:

http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html (if you do not have C++11)

I know you said you are using C++98, but if you had the luxury of C++11, then here are some more options:
http://en.cppreference.com/w/cpp/utility/functional/function (if you have C++11)
http://en.cppreference.com/w/cpp/utility/functional/bind (if you have C++11)

Hope this helps. Definitely feel free to ask if this raises another question :)

from easywsclient.

wezside avatar wezside commented on August 10, 2024

That's awesome. Didn't know about Functors and their use. Thanks!

from easywsclient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.