member function example 2: pointer to member functions is a member function example.
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: ./CppMemberFunctionExample2/CppMemberFunctionExample2.pro
TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp # # # Type of compile # # CONFIG(release, debug|release) { DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ unix { QMAKE_CXXFLAGS += -Werror }
#include <iostream> void SayA() { std::cout << "A\n"; } void SayB() { std::cout << "B\n"; } struct Person { void SayBye() const noexcept { std::cout << "Bye\n"; } void SayHello() const noexcept { std::cout << "Hello\n"; } }; int main() { //Ordinay pointer to functions { typedef void (*Function)(); const Function a = SayA; const Function b = SayB; a(); b(); Function c = SayA; c(); c = SayB; c(); } //Pointer to member functions { typedef void (Person::*MemberFunction)() const; //Note: do not add noexcept const Person p; const MemberFunction a = &Person::SayHello; ((&p)->*a)(); const MemberFunction b = &Person::SayBye; ((&p)->*b)(); MemberFunction c = &Person::SayHello; ((&p)->*c)(); c = &Person::SayBye; ((&p)->*c)(); } } /* Screen output A B A B Hello Bye Hello Bye Press <RETURN> to close this window... */

