Skip to content

Instantly share code, notes, and snippets.

@luchenqun
Created December 19, 2017 14:52
Show Gist options
  • Save luchenqun/38858812550a273e2e19ef80e6ad6f14 to your computer and use it in GitHub Desktop.
Save luchenqun/38858812550a273e2e19ef80e6ad6f14 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class BirdType //鸟类
{
};
class InsectType //昆虫类
{
};
class BeastType //走兽类
{
};
template <typename A>
class Traits
{
};
template <>
class Traits<BirdType>
{
public:
void fly()
{
cout << "Birder fly " << endl;
}
};
template <>
class Traits<InsectType>
{
public:
void fly()
{
cout << "Insect fly " << endl;
}
};
template <>
class Traits<BeastType>
{
public:
void fly()
{
cout << "Beast can't fly " << endl;
}
};
template <typename T>
class Animal
{
public:
void Fly()
{
Traits<T>().fly();
};
};
int main()
{
Animal<BirdType> brider;
brider.Fly();
Animal<InsectType> insect;
insect.Fly();
Animal<BeastType> beast;
beast.Fly();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment