Advertisement
atomsymbol

C++ operator overriding

Mar 5th, 2021
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct A {
  4.     A();
  5.     virtual bool operator==(const A&) const { printf("A==A\n"); return false; }
  6. };
  7.  
  8. struct B : public A {
  9.             bool operator==(const A&) const override { printf("B==A\n"); return false; }
  10.     virtual bool operator==(const B&) const          { printf("B==B\n"); return false; }
  11. };
  12.  
  13. struct C final : public B {
  14.     bool operator==(const B&) const override { printf("C==B\n"); return false; }
  15.     bool operator==(const C&) const          { printf("C==C\n"); return false; }
  16. };
  17.  
  18. A::A() {
  19.     static bool comparing = false;
  20.     if(!comparing) {
  21.         comparing = true;
  22.         *this == B();
  23.         comparing = false;
  24.     }
  25. }
  26.  
  27. int main() {
  28. //  const A &a = B();
  29. //  a == B(); printf("\n");
  30. //  B() == a; printf("\n");
  31.     B() == B(); printf("\n");
  32. //  B() == C(); printf("\n");
  33. //  C() == B(); printf("\n");
  34.     C() == C(); printf("\n");
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement