home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!walter!snoopy!ik
- From: ik@snoopy.ctt.bellcore.com (Ik Su Yoo)
- Subject: virtual madness - problem with virtual base class
- Message-ID: <1992Jul24.190709.10315@walter.bellcore.com>
- Sender: news@walter.bellcore.com
- Nntp-Posting-Host: snoopy.ctt.bellcore.com
- Reply-To: ik@ctt.bellcore.com (Ik Su Yoo)
- Organization: Bell Communications Research
- Date: Fri, 24 Jul 92 19:07:09 GMT
- Lines: 39
-
- I'm using cfront 2.1 with ObjectCenter 1.1 and having problem with the
- following code:
-
- #include <iostream.h>
-
- class A {
- public:
- virtual void f() { cout << "A.f()" << endl; };
- };
-
- class B: public virtual A {
- };
-
- main()
- {
- B bb;
- void* bbp = &bb;
- ((B*)bbp)->f();
- ((A*)bbp)->f();
- }
-
- The program, when compiled and ran, leads to segmentation fault at the last
- line in main(). Interpreting the code from ObjectCenter gives me an
- "illegal array index" error.
-
- When I change "public virtual A" to "public A", it runs fine. However, I
- need to derive from A as public virtual, because eventually my class
- hierarchy will be a DAG with A as the root.
-
- Is this not a legal thing to do in C++? I thought I was allowed to cast to
- void* and back. I also tried the above code with g++ and it also gave me
- segmentation fault.
-
- Any insight or solution to this problem is appreciated. Thanks.
-
-
- Ik S. Yoo
- RRC-1H229, x5764
- ik@ctt.bellcore.com
-