home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19773 < prev    next >
Encoding:
Text File  |  1993-01-23  |  2.8 KB  |  99 lines

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: operator = with inheritance
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. References: <1993Jan20.221834.26489@murdoch.acc.Virginia.EDU> <1993Jan14.184649.17113@ucc.su.OZ.AU>
  7. Distribution: world
  8. X-Mailer: cppnews $Revision: 1.31 $
  9. Organization: Trumphurst Ltd.
  10. Lines: 84
  11. Date: Fri, 22 Jan 1993 15:13:40 +0000
  12. Message-ID: <727740820snx@trmphrst.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <1993Jan20.221834.26489@murdoch.acc.Virginia.EDU> gs4t@virginia.edu (Gnanasekaran Swaminathan) writes:
  16. > nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
  17. > : But this unfortunately fails the test from the previous posting ...
  18. > It works fine. Note the assign op is also virtual.
  19. > Since assign op is virtual, B::op = (a2) is called for
  20. > reference a1. B::op = (a2), in turn, calls B::copy2 (this).
  21. > g++ compiler gets this wrong. Probably try a different compiler
  22. > if you are using g++.
  23. I am afraid I think it is YOU who has got it wrong !
  24.  
  25. I tested the program at the end of this post under Zortech c++ 3.0r4,
  26. Borland C++ 3.0, and Microsoft C7, and they all gave the following output ...
  27.  
  28. B::=(A&)
  29. B::copy2(A*)
  30. A::copy2(A*)
  31. b1.b = 11 b2.b = 22 b1.a = 2 b2.a = 2
  32.  
  33. If you follow this through,
  34. a1=a2; calls the virtual operator B::operator=(const A&)
  35. B::operator=(const A&) calls virtual function B::copy2(A*), NOT B::copy2(B*),
  36.   because polymorphism doesn't act on function arguments.
  37.  
  38. Did you actually test this on some compiler ? If so, which one ?
  39. _I_ think it has a bug !
  40. -- 
  41. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  42. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  43. ----------------------- cut here -------------------------
  44. #include <iostream.h>
  45.  
  46. class A {
  47. public:
  48.   int a;
  49.   virtual void copy2 (A* c) const { c->a = a; cout << "A::copy2(A*)\n"; }
  50.   A (int c): a(c) {}
  51.   virtual A& operator = (const A& c) {
  52.       if (this != &c) { // testing for assignment to the same object
  53.         cout << "A::=(A&)\n"; 
  54.     c.copy2 (this);
  55.       }
  56.       return *this;
  57.   }
  58. };
  59.  
  60. class B: public A {
  61. public:
  62.   int b;
  63.   virtual void copy2 (A* c) const { cout << "B::copy2(A*)\n"; A::copy2 (c); }
  64.   virtual void copy2 (B* c) const { cout << "B::copy2(B*)\n"; A::copy2 (c); c->b = b; }
  65.   B (int c): A(c), b(2*c) {}
  66.   A& operator = (const A& c) {
  67.      if (this != &c) {
  68.        cout << "B::=(A&)\n";
  69.        c.copy2 (this);
  70.      }
  71.      return *this;
  72.   }
  73. };
  74.  
  75. main()
  76. {
  77.     B b1(1);
  78.     B b2(2);
  79.  
  80.     b1.a=1;
  81.     b1.b=11;
  82.     b2.a=2;
  83.     b2.b=22;
  84.     
  85.     A &a1=b1;
  86.     A &a2=b2;
  87.  
  88.     a1=a2; // OH! only assigns the 'A' subobject of B.
  89.              // A::op= (a), A::copy2 (A* &a)
  90.     cout << "b1.b = " << b1.b << " b2.b = " << b2.b << " b1.a = " << b1.a <<
  91.         " b2.a = " << b2.a << "\n";
  92.     return 0;
  93. }
  94.  
  95.  
  96.