Guest User

Untitled

a guest
Nov 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. template<typename T>
  2. class A {
  3. private:
  4. int a = 0;
  5. public:
  6. void func(A<double> second) {
  7. cout << second.a;
  8. }
  9. };
  10.  
  11.  
  12. void main()
  13. {
  14. A<int> first;
  15. A<double> second;
  16. first.func(second);
  17. }
  18.  
  19. template<typename T>
  20. class A {
  21. private:
  22. int a = 0;
  23. public:
  24. friend void A::func(A<double> second);
  25. void func(A<double> second) {
  26. cout << second.a;
  27. }
  28. };
  29.  
  30. public:
  31. int a = 0;
  32.  
  33. template <typename> friend class A;
  34.  
  35. template <typename U> friend void A<U>::func(A<double>);
  36.  
  37. template<typename T> class A {
  38. private:
  39. int a = 0;
  40. public:
  41. void func(A<double>);
  42. };
  43.  
  44. template<> class A<double> {
  45. private:
  46. int a = 0;
  47. public:
  48. template <typename U> friend void A<U>::func(A<double>);
  49.  
  50. void func(A second) {
  51. cout << second.a;
  52. }
  53. };
  54.  
  55. template<typename T>
  56. inline void A<T>::func(A<double> second) {
  57. cout << second.a;
  58. }
  59.  
  60. #include <iostream>
  61.  
  62. template<typename T>
  63. class node {
  64. public:
  65. explicit node(T _value)
  66. : m_value(_value)
  67. {}
  68.  
  69. public:
  70. template <typename U>
  71. void add_child(node<U>& _node) {
  72. // логика, не относящаяся к примеру
  73. std::clog << "добавлена дочерняя нода [value=" << _node.m_value << "]n";
  74. }
  75.  
  76. protected:
  77. T m_value;
  78.  
  79. template <typename U>
  80. friend class node;
  81. };
  82.  
  83. int main() {
  84. node<int> node_i { 1 };
  85. node<long> node_l { 2 };
  86. node<short> node_s { 3 };
  87.  
  88. node_i.add_child(node_l);
  89. node_l.add_child(node_s);
  90. return 0;
  91. }
Add Comment
Please, Sign In to add comment