testpolymorph.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /** testpolymorph.cpp --- A sequence of polymorphism examples.
  2. *
  3. * Copyright (C) 2009-2012 Free Software Foundation, Inc.
  4. *
  5. * Author: Eric M. Ludlam <eric@siege-engine.com>
  6. *
  7. * This file is part of GNU Emacs.
  8. *
  9. * GNU Emacs is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GNU Emacs is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <cmath>
  23. // Test 1 - Functions w/ prototypes
  24. namespace proto {
  25. int pt_func1(int arg1);
  26. int pt_func1(int arg1) {
  27. return 0;
  28. }
  29. }
  30. // Test 2 - Functions w/ different arg lists.
  31. namespace fcn_poly {
  32. int pm_func(void) {
  33. return 0;
  34. }
  35. int pm_func(int a) {
  36. return a;
  37. }
  38. int pm_func(char a) {
  39. return int(a);
  40. }
  41. int pm_func(double a) {
  42. return int(floor(a));
  43. }
  44. }
  45. // Test 3 - Methods w/ different arg lists.
  46. class meth_poly {
  47. public:
  48. int pm_meth(void) {
  49. return 0;
  50. }
  51. int pm_meth(int a) {
  52. return a;
  53. }
  54. int pm_meth(char a) {
  55. return int(a);
  56. }
  57. int pm_meth(double a) {
  58. return int(floor(a));
  59. }
  60. };
  61. // Test 4 - Templates w/ partial specifiers.
  62. namespace template_partial_spec {
  63. template <typename T> class test
  64. {
  65. public:
  66. void doSomething(T t) { };
  67. };
  68. template <typename T> class test<T *>
  69. {
  70. public:
  71. void doSomething(T* t) { };
  72. };
  73. }
  74. // Test 5 - Templates w/ full specialization which may or may not share
  75. // common functions.
  76. namespace template_full_spec {
  77. template <typename T> class test
  78. {
  79. public:
  80. void doSomething(T t) { };
  81. void doSomethingElse(T t) { };
  82. };
  83. template <> class test<int>
  84. {
  85. public:
  86. void doSomethingElse(int t) { };
  87. void doSomethingCompletelyDifferent(int t) { };
  88. };
  89. }
  90. // Test 6 - Dto., but for templates with multiple parameters.
  91. namespace template_multiple_spec {
  92. template <typename T1, typename T2> class test
  93. {
  94. public:
  95. void doSomething(T1 t) { };
  96. void doSomethingElse(T2 t) { };
  97. };
  98. template <typename T2> class test<int, T2>
  99. {
  100. public:
  101. void doSomething(int t) { };
  102. void doSomethingElse(T2 t) { };
  103. };
  104. template <> class test<float, int>
  105. {
  106. public:
  107. void doSomething(float t) { };
  108. void doSomethingElse(int t) { };
  109. void doNothing(void) { };
  110. };
  111. }
  112. // End of polymorphism test file.