Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BoostFunctionExample3

 

BoostQt CreatorLubuntu

 

Boost.Function example 3: calling a member function with an argument and reading its result is a Boost.Function example that shows how to call a member function with an argument and how to read its result.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppBoostFunctionExample3/CppBoostFunctionExample3.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

include(../../Libraries/Boost.pri) #Or use the code below
# win32 {
#   INCLUDEPATH += \
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp

 

 

 

 

 

./CppBoostFunctionExample3/main.cpp

 

#include <cassert>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <boost/function.hpp>
#pragma GCC diagnostic pop

struct MyClass
{
  MyClass(const int x = 0) : m_x(x) {}
  int Plus( const int x) const { return m_x + x; }
  int Minus(const int x) const { return m_x - x; }
  int m_x;
};

int main()
{
  typedef boost::function<int(const MyClass*,int)> Function;
  MyClass c;
  Function f1 = &MyClass::Plus;
  Function f2 = &MyClass::Minus;
  assert( f1(&c,1000) ==  1000 );
  assert( f2(&c,1000) == -1000 );
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml