(C++) HostClassExample1
February 24, 2017 · View on GitHub
(C++) HostClassExample1
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: ./CppHostClassExample1/CppHostClassExample1.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 # } SOURCES += main.cpp
./CppHostClassExample1/main.cpp
#include <iostream> struct HappySayHello { void SayHello() const { std::cout << "Hello!\n"; } protected: ~HappySayHello() { // The destructor of a policy class should be protected and non-virtual: // * Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, // guidelines, and best practices. ISBN: 0-32-111358-6. Item 50: 'Make // base class destructors public and virtual, or protected and nonvirtual' // * Andrei Alexandrescu. Modern C++ Design. 2001. ISBN: 0201704315. // Page 13. Section 1.7: 'The lightweight, effective solution that // policies should use is to define a nonvirtual protected destructor' } }; struct GrumpySayHello { void SayHello() const { std::cout << "Moi\n"; } protected: ~GrumpySayHello() { // The destructor of a policy class should be protected and non-virtual: // * Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, // guidelines, and best practices. ISBN: 0-32-111358-6. Item 50: 'Make // base class destructors public and virtual, or protected and nonvirtual' // * Andrei Alexandrescu. Modern C++ Design. 2001. ISBN: 0201704315. // Page 13. Section 1.7: 'The lightweight, effective solution that // policies should use is to define a nonvirtual protected destructor' } }; template <class SayHelloPolicy> class HostClass : public SayHelloPolicy { //Obtains its member functions from its SayHelloPolicy }; int main() { HostClass<HappySayHello> happy; happy.SayHello(); HostClass<GrumpySayHello> grumpy; grumpy.SayHello(); }