home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / fixed300.arj / VJG0625A.CPP < prev   
Encoding:
C/C++ Source or Header  |  1991-06-26  |  1.9 KB  |  114 lines

  1. #if 0
  2. From: Victor Griswold
  3. Subject: operator=()
  4. Status: Fixed in 3.0
  5. #endif
  6.  
  7. //        ZTC 3.0b7 ignores class type conversion operators when resolving
  8. //    class assignment operators.  This causes bit-image copies to be employed
  9. //    for assignments instead of the the proper assignment operators declared
  10. //    in the class.  This worked correctly in all ZTC compilers 2.14-3.0b5, in
  11. //    GCC, and in AT&T CC.  The fault is utterly fatal for all my reference-
  12. //    counting storage allocators.
  13. //
  14. //        The following code SHOULD output:
  15. //
  16. //            in s1::operator int()
  17. //            in s1& operator=(int src)
  18. //            in s1::operator int()
  19. //            in s1& operator=(int src)
  20. //            in s2& operator=(const s2 &src)
  21. //            in s2::operator int()
  22. //            in s2& operator=(int src)
  23. //
  24. //        Instead, it outputs just:
  25. //
  26. //            in s1::operator int()
  27. //            in s1& operator=(int src)
  28. //            in s2& operator=(const s2 &src)
  29. //            in s2::operator int()
  30. //            in s2& operator=(int src)
  31. //
  32. //        This is because it performs an incorrect bit-image copy of the
  33. //    assignment 'b = a'.
  34. //
  35.  
  36. #include <stdio.h>
  37.  
  38.  
  39. struct s1 {
  40.  
  41.     int x;
  42.  
  43.     operator int() const;
  44.  
  45.     s1& operator=(int src);
  46. };
  47.  
  48.  
  49. s1::operator int() const {
  50.     printf("in s1::operator int()\n");
  51.     return x;
  52. }
  53.  
  54.  
  55. s1& s1::operator=(int src) {
  56.     printf("in s1& operator=(int src)\n");
  57.      x = src;
  58.     return *this;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. struct s2 {
  65.     int x;
  66.  
  67.     operator int() const;
  68.  
  69.      s2& operator=(int src);
  70.  
  71.     s2& operator=(const s2 &src);
  72. };
  73.  
  74.  
  75. s2::operator int() const {
  76.     printf("in s2::operator int()\n");
  77.     return x;
  78. }
  79.  
  80.  
  81. s2& s2::operator=(int src) {
  82.     printf("in s2& operator=(int src)\n");
  83.      x = src;
  84.     return *this;
  85. }
  86.  
  87.  
  88. s2& s2::operator=(const s2 &src) {
  89.     printf("in s2& operator=(const s2 &src)\n");
  90.     x = src.x;
  91.     return *this;
  92. }
  93.  
  94.  
  95.  
  96. int main() {
  97.      s1 a, b;
  98.     s2 c, d;
  99.  
  100.  
  101.     a.x = 1;
  102.     b.x = 2;
  103.     c.x = 3;
  104.     d.x = 4;
  105.  
  106.     b = a;
  107.     b = int(a);
  108.  
  109.     d = c;
  110.     d = int(c);
  111.  
  112.     return 0;
  113. }
  114.