bhupixb

Understanding friend functions

Jul 6th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct mint {
  5. private:
  6. int val;
  7. static int const mod = 7;
  8. public:
  9.  
  10. mint(int x) : val(x) {
  11. cout << "Int constructor called with " << x << '\n';
  12. }
  13. mint() {val = 0;}
  14. mint(mint &other) {
  15. val = other.val;
  16. cout << "Copy constructor called " << val << '\n';
  17. }
  18.  
  19. // 13 + ins means 13 ko construct krega mint me and then call this
  20. friend mint operator+(mint a, mint const b) {
  21. a.val += b.val;
  22. cout << "+ operator called " << b.val << '\n';
  23. return a;
  24. }
  25. // https://softwareengineering.stackexchange.com/a/68888
  26. friend mint operator+(int x, mint other) {
  27. mint tmp(x);
  28. tmp.val += other.val;
  29. cout << "+ operator called " << other.val << '\n';
  30. return tmp;
  31. }
  32. };
  33.  
  34. // or
  35. // mint operator+(int x, mint other) {
  36. // mint tmp(x);
  37. // tmp.val += other.val;
  38. // cout << "+ operator called " << other.val << '\n';
  39. // return tmp;
  40. // }
  41.  
  42. int main() {
  43. mint ins(10);
  44. ins = 23 - 13 + ins + 13;
  45.  
  46. return 0;
  47. }#include <bits/stdc++.h>
  48. using namespace std;
  49.  
  50. struct mint {
  51. private:
  52. int val;
  53. static int const mod = 7;
  54. public:
  55.  
  56. mint(int x) : val(x) {
  57. cout << "Int constructor called with " << x << '\n';
  58. }
  59. mint() {val = 0;}
  60. mint(mint &other) {
  61. val = other.val;
  62. cout << "Copy constructor called " << val << '\n';
  63. }
  64.  
  65. // 13 + ins means 13 ko construct krega mint me and then call this
  66. friend mint operator+(mint a, mint const b) {
  67. a.val += b.val;
  68. cout << "+ operator called " << b.val << '\n';
  69. return a;
  70. }
  71. // https://softwareengineering.stackexchange.com/a/68888
  72. friend mint operator+(int x, mint other) {
  73. mint tmp(x);
  74. tmp.val += other.val;
  75. cout << "+ operator called " << other.val << '\n';
  76. return tmp;
  77. }
  78. };
  79.  
  80. // or
  81. // mint operator+(int x, mint other) {
  82. // mint tmp(x);
  83. // tmp.val += other.val;
  84. // cout << "+ operator called " << other.val << '\n';
  85. // return tmp;
  86. // }
  87.  
  88. int main() {
  89. mint ins(10);
  90. ins = 23 - 13 + ins + 13;
  91.  
  92. return 0;
  93. }#include <bits/stdc++.h>
  94. using namespace std;
  95.  
  96. struct mint {
  97. private:
  98. int val;
  99. static int const mod = 7;
  100. public:
  101.  
  102. mint(int x) : val(x) {
  103. cout << "Int constructor called with " << x << '\n';
  104. }
  105. mint() {val = 0;}
  106. mint(mint &other) {
  107. val = other.val;
  108. cout << "Copy constructor called " << val << '\n';
  109. }
  110.  
  111. // 13 + ins means 13 ko construct krega mint me and then call this
  112. friend mint operator+(mint a, mint const b) {
  113. a.val += b.val;
  114. cout << "+ operator called " << b.val << '\n';
  115. return a;
  116. }
  117. // https://softwareengineering.stackexchange.com/a/68888
  118. friend mint operator+(int x, mint other) {
  119. mint tmp(x);
  120. tmp.val += other.val;
  121. cout << "+ operator called " << other.val << '\n';
  122. return tmp;
  123. }
  124. };
  125.  
  126. // or
  127. // mint operator+(int x, mint other) {
  128. // mint tmp(x);
  129. // tmp.val += other.val;
  130. // cout << "+ operator called " << other.val << '\n';
  131. // return tmp;
  132. // }
  133.  
  134. int main() {
  135. mint ins(10);
  136. ins = 23 - 13 + ins + 13;
  137.  
  138. return 0;
  139. }
Add Comment
Please, Sign In to add comment