Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. //
  2. // Overriding a member method of a template class/struct by defining a specialized implementation.
  3. //
  4.  
  5. #include <cstdio>
  6. #include <typeinfo>
  7.  
  8. template<typename T>
  9. struct Foo
  10. {
  11. void bar()
  12. {
  13. std::printf("Base Foo<%s>::bar()\n", typeid(T).name());
  14. }
  15. };
  16.  
  17. template<>
  18. void Foo<int>::bar()
  19. {
  20. std::printf("Specialized Foo<int>::bar()\n");
  21. }
  22.  
  23. template<>
  24. void Foo<float>::bar()
  25. {
  26. std::printf("Specialized Foo<float>::bar()\n");
  27. }
  28.  
  29. int main()
  30. {
  31. Foo<char> cFoo;
  32. Foo<int> iFoo;
  33. Foo<float> fFoo;
  34.  
  35. cFoo.bar(); // Base implementation
  36. iFoo.bar(); // Specialized
  37. fFoo.bar(); // Specialized
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement