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



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