Difference between revisions of "CPP/Functor"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Functor.cpp== <source lang="cpp"> #include <iostream> struct Function { virtual double operator() (const double x, const double& y) const = 0; }; struct Add : public Functi…')
 
(No difference)

Latest revision as of 11:54, 25 February 2011

Functor.cpp

#include <iostream>
 
struct Function
{
  virtual double operator() (const double x, const double& y) const = 0;
};
 
struct Add : public Function
{
  double operator() (const double x, const double& y) const
  {
    return x+y;
  }
};
 
struct Multiply : public Function
{
  double operator() (const double x, const double& y) const
  {
    return x*y;
  }
};
 
int main(int argc, char *argv[])
{
  Function* a;
  a = new Add;
  std::cout << (*a)(2.,3.) << std::endl;
  delete a;
 
  a = new Multiply;
  std::cout << (*a)(2.,3.) << std::endl;
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Functor)
 
ADD_EXECUTABLE(Functor Functor.cpp )