home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11490 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.4 KB  |  52 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!walter!snoopy!ik
  3. From: ik@snoopy.ctt.bellcore.com (Ik Su Yoo)
  4. Subject: virtual madness - problem with virtual base class
  5. Message-ID: <1992Jul24.190709.10315@walter.bellcore.com>
  6. Sender: news@walter.bellcore.com
  7. Nntp-Posting-Host: snoopy.ctt.bellcore.com
  8. Reply-To: ik@ctt.bellcore.com (Ik Su Yoo)
  9. Organization: Bell Communications Research
  10. Date: Fri, 24 Jul 92 19:07:09 GMT
  11. Lines: 39
  12.  
  13. I'm using cfront 2.1 with ObjectCenter 1.1 and having problem with the
  14. following code:
  15.  
  16.     #include <iostream.h>
  17.  
  18.     class A {
  19.     public:
  20.       virtual void f() { cout << "A.f()" << endl; };
  21.     };
  22.  
  23.     class B: public virtual A {
  24.     };
  25.  
  26.     main()
  27.     {
  28.       B bb;
  29.       void* bbp = &bb;
  30.       ((B*)bbp)->f();
  31.       ((A*)bbp)->f();
  32.     }
  33.  
  34. The program, when compiled and ran, leads to segmentation fault at the last
  35. line in main(). Interpreting the code from ObjectCenter gives me an
  36. "illegal array index" error.
  37.  
  38. When I change "public virtual A" to "public A", it runs fine. However, I
  39. need to derive from A as public virtual, because eventually my class
  40. hierarchy will be a DAG with A as the root.
  41.  
  42. Is this not a legal thing to do in C++? I thought I was allowed to cast to
  43. void* and back. I also tried the above code with g++ and it also gave me
  44. segmentation fault.
  45.  
  46. Any insight or solution to this problem is appreciated. Thanks.
  47.  
  48.  
  49. Ik S. Yoo
  50. RRC-1H229, x5764
  51. ik@ctt.bellcore.com
  52.