Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

callback server example

#include <functional>
#include <iostream>

struct Server {
   void print_add(int i) const { std::cout << i << '\n'; }
   int num_;
   
   std::function<void(Server*)> callback_function_t[10];
   int count = 0;

   void addSite(std::function<void(Server*)> func)
   {
       callback_function_t[count++]=func;
   }
  
   void config()
   { 
       print_add(0);
       for(int i=0;i<10;i++)
       if(callback_function_t[i])
       callback_function_t[i](this); 
       
   };
};
 

struct Site 
{
   void call(Server *server)
   {
       server->print_add(1);
    };
};

struct Site2 
{
   void call(Server *server)
   {
       server->print_add(2);
    };
};


int main() {
    
    Site site;
    Site2 site2;
    
    Server server;
    using std::placeholders::_1;
    server.addSite(std::bind(&Site::call,&site,_1));
    server.addSite(std::bind(&Site2::call,&site2,_1));
    server.config();

}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.