(C++) StdFunctionExample3
February 24, 2017 · View on GitHub
(C++) StdFunctionExample3
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
Qt project file: ./CppStdFunctionExample3/CppStdFunctionExample3.pro
TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp # # # Type of compile # # CONFIG(debug, debug|release) { message(Debug mode) } CONFIG(release, debug|release) { message(Release mode) #Remove all asserts and TRACE DEFINES += NDEBUG NTRACE_BILDERBIKKEL } # # # Platform specific # # # # # Compiler flags # # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra unix { QMAKE_CXXFLAGS += -Werror }
./CppStdFunctionExample3/main.cpp
#include <iostream> #include <functional> struct Speaker { Speaker() {} void SayHello(const std::string& name) const noexcept { std::cout << "Hello " << name << '\n'; } void SayBye(const std::string& name) const noexcept { std::cout << "Bye " << name << '\n'; } }; int main() { const Speaker s1; const Speaker s2; const std::function<void (const Speaker*, const std::string&)> say_hello_function = &Speaker::SayHello; const std::function<void (const Speaker*, const std::string&)> say_bye_function = &Speaker::SayBye; say_hello_function(&s1,"speaker 1"); say_bye_function(&s1,"speaker 1"); say_hello_function(&s2,"speaker 2"); say_bye_function(&s2,"speaker 2"); } /* Screen output: Hello speaker 1 Bye speaker 1 Hello speaker 2 Bye speaker 2 Press <RETURN> to close this window... */