home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13457 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.6 KB  |  66 lines

  1. Xref: sparky comp.lang.c++:13457 comp.std.c++:1144
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!ftpbox!motsrd!news
  4. From: shang@corp.mot.com (David (Lujun) Shang)
  5. Subject: Downcast and Reverse Offset
  6. Message-ID: <1992Sep10.015857.4050@cadsun.corp.mot.com>
  7. Sender: news@cadsun.corp.mot.com
  8. Reply-To: shang@corp.mot.com
  9. Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
  10. Date: Thu, 10 Sep 92 01:58:57 GMT
  11. Lines: 53
  12.  
  13.  
  14. The topic has been discussed sometime before.
  15.  
  16. I got an email from someone to point out to me that reverse offset is not
  17. required for downcast. And I replied to agreed to this point of view at 
  18. that time. But thinking over the problem today, I find that reverse offset 
  19. is still necessary. 
  20.  
  21. [I'm sorry I lost your email so I cannot reply to you directly.]
  22.  
  23. Consider the following example:
  24.  
  25. class A {};
  26. class B1:A {};
  27. class B2:A {};
  28. class C: B1,B2 {};
  29.  
  30. C c;
  31. B1 * b1p;
  32. B2 * b2p;
  33. A * ap1, * ap2;
  34. C * cp1, * cp2;
  35.  
  36. b1p = &c;
  37. b2p = &c;
  38. ap1 = b1p;   // ap1 is pointing to the B1::A base of the object c
  39. ap2 = b2p;   // ap2 is pointing to the B2::A base of the object c
  40.  
  41. The problem is that how the compiler knows which base of the object the base  
  42. pointer is pointing to:
  43.  
  44. cp1 = (C*) ap1;
  45. cp2 = (C*) ap2;
  46.  
  47. Therefore, reverse offset is still necessary.
  48.  
  49. It is quite ridiculous that BC++ V3.1 does not allow the type cast in
  50.  
  51. cp1 = (C*) ap1;
  52. cp2 = (C*) ap2;
  53.  
  54. where both ap1 and ap2 are actually pointing to an object of C! On the other  
  55. hand, cast from an arbitrary class is allowed:
  56.  
  57. cp1 = (AnyOtherClass1 *) anyOtherObjectPointer1;
  58. cp2 = (AnyOtherClass2 *) anyOtherObjectPointer2;
  59.  
  60. ???!!!
  61.  
  62. David Shang
  63.  
  64.  
  65.  
  66.