(C++) TemplateClassExample4
February 24, 2017 · View on GitHub
(C++) TemplateClassExample4



Template class example 4: class that has an enum class template type is a template class example.
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: ./CppTemplateClassExample4/CppTemplateClassExample4.pro
include(../../ConsoleApplication.pri) SOURCES += main.cpp
./CppTemplateClassExample4/main.cpp
#include <iostream> enum class PersonType { happy, grumpy }; template <PersonType T> struct Person { ///Every Person<T> can say hello void SayHello() const; }; //Person<PersonType::happy> says hello in a happy way template <> void Person<PersonType::happy>::SayHello() const { std::cout << "Hello!\n"; } //Person<PersonType::grumpy> says hello in a grumpy way template <> void Person<PersonType::grumpy>::SayHello() const { std::cout << "Moi\n"; } int main() { const Person<PersonType::happy> p; p.SayHello(); const Person<PersonType::grumpy> q; q.SayHello(); }