Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) MemberFunctionExample2

 

C++11STLQt CreatorLubuntu

 

member function example 2: pointer to member functions is a member function example.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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
}

 

 

 

 

 

./CppMemberFunctionExample2/main.cpp

 

#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...

*/

 

 

 

 

 

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