Created
December 10, 2016 10:41
-
-
Save hryniuk/5d302795d3d7291d28670fbadbe89360 to your computer and use it in GitHub Desktop.
Basic class parametrization example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<typename T, typename S> | |
struct Citizen | |
{ | |
Citizen(T k, S l) : k_{k}, l_{l} {} | |
T k_; | |
S l_; | |
}; | |
template<typename T> | |
using Adult = Citizen<T, int>; | |
template<typename T> | |
using Teenager = Citizen<T, double>; | |
template<typename T> | |
using Sheriff = Citizen<T, char>; | |
template<typename T> | |
struct Citizen<T, char> | |
{ | |
Citizen(T k, char l) : k_{k}, l_{l} { | |
std::cout | |
<< "### O szeryf, szeryf, ty trzymaj sie\n" | |
<< "### Tak bardzo dobrze nie jest, ale nie jest zle\n"; | |
} | |
T k_; | |
char l_; | |
}; | |
int main() | |
{ | |
Adult<std::string> a("adult", 10); | |
Teenager<double> b(3.14, 2.72); | |
Sheriff<double> c(3.14, 'a'); | |
std::cout << a.k_ << " " << a.l_ << "\n"; | |
std::cout << b.k_ << " " << b.l_ << "\n"; | |
std::cout << c.k_ << " " << c.l_ << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment