topical media & game development

talk show tell print

professional-program-16-Functors-Functors.c

? / professional-program-16-Functors-Functors.c


  include <string>
  using namespace std;
  
  class FunctionObject
  {
  public:
    int operator() (int inParam); // function-call operator
    void operator() (string& str) {}
    int aMethod(int inParam); // normal method
  };
  
  //Implementation of overloaded function-call operator
  int FunctionObject::operator() (int inParam)
  {
    return (inParam * inParam);
  }
  
  // Implementation of normal method
  int FunctionObject::aMethod(int inParam)
  {
    return (inParam * inParam);
  }
  
  int main(int argc, char** argv)
  {
    int x = 3, xSquared, xSquaredAgain;
    FunctionObject square;
  
    xSquared = square(x); // Call the function-call operator
    xSquaredAgain = square.aMethod(x); // Call the normal method
  
    return (0);
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.