Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TemplateClassExample4

 

STLQt CreatorWindows

 

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)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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();
}

 

 

 

 

 

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