CPP/Boost/ClassFunctionPointer

From ProgrammingExamples
< CPP
Jump to: navigation, search

ClassFunctionPointer.cpp

#include <boost/function.hpp>
#include <boost/bind.hpp>
 
#include <iostream>
 
class MyClass
{
private:
  double Update1()
  {
    std::cout << "Update1" << std::endl;
  }
  double Update2()
  {
    std::cout << "Update2" << std::endl;
  }
 
public:
  boost::function< double() > Update;
  void SetUpdateMethod(int method)
  {
    if(method == 1)
    {
      this->Update = boost::bind(&MyClass::Update1,this);
    }
    if(method == 2)
    {
      this->Update = boost::bind(&MyClass::Update2,this);
    }
  }
};
 
int main () 
{
  MyClass a;
  a.SetUpdateMethod(1);
  a.Update();
 
  a.SetUpdateMethod(2);
  a.Update();
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(ClassFunctionPointer)
ADD_EXECUTABLE(ClassFunctionPointer ClassFunctionPointer.cpp)