Difference between revisions of "CPP/Boost/Function"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Function.cpp== <source lang="cpp"> #include <boost/function.hpp> #include <boost/bind.hpp> #include <iostream> class MyClass { private: double Update1() { std::cout <…')
 
 
Line 3: Line 3:
 
#include <boost/function.hpp>
 
#include <boost/function.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/bind.hpp>
 
+
 
#include <iostream>
 
#include <iostream>
  
class MyClass
+
double Function0()
 
{
 
{
private:
+
     return 1;
  double Update1()
+
}
  {
+
 
     std::cout << "Update1" << std::endl;
+
double Function1(const int a)
  }
+
{
  double Update2()
+
     return a+1;
  {
+
}
     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 ()  
 
int main ()  
 
{
 
{
   MyClass a;
+
   boost::function<double ()> FunctionPointer0 = &Function0;
  a.SetUpdateMethod(1);
+
   boost::function<double (const int)> FunctionPointer1 = &Function1;
   a.Update();
+
  
  a.SetUpdateMethod(2);
 
  a.Update();
 
 
 
 
   return 0;
 
   return 0;
 
}
 
}
 +
 
</source>
 
</source>
  
Line 52: Line 32:
 
Project(Function)
 
Project(Function)
 
ADD_EXECUTABLE(Function Function.cpp)
 
ADD_EXECUTABLE(Function Function.cpp)
 
 
  
 
</source>
 
</source>

Latest revision as of 15:37, 12 May 2015

Function.cpp

#include <boost/function.hpp>
#include <boost/bind.hpp>
 
#include <iostream>
 
double Function0()
{
    return 1;
}
 
double Function1(const int a)
{
    return a+1;
}
 
int main () 
{
  boost::function<double ()> FunctionPointer0 = &Function0;
  boost::function<double (const int)> FunctionPointer1 = &Function1;
 
  return 0;
}

CMakeLists.txt

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