home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!slsvaat!us-es.sel.de!reindorf
- From: reindorf@us-es.sel.de (Charles Reindorf)
- Subject: Re: Does anyone understand this quirk?
- Message-ID: <1992Nov17.115630.7704@us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL-Alcatel Line Transmission Systems Dept. US/ES
- References: <1992Nov17.073603.5235@murdoch.acc.Virginia.EDU>
- Date: Tue, 17 Nov 92 11:56:30 GMT
- Lines: 51
-
- In article <1992Nov17.073603.5235@murdoch.acc.Virginia.EDU>, rad2r@uvacs.cs.Virginia.EDU (Robert DeLine) writes:
- |>
- |> For your amusement, please consider the following little program:
- |>
- |>
- |> #include <iostream.h>
- |>
- |> class A
- |> {
- |> public:
- |> A () { this->id(); }
- |> virtual void id () const { cout << "A\n"; }
- |> };
- |>
- |>
- |> class B : public A
- |> {
- |> public:
- |> B () : A() { }
- |> virtual void id () const { cout << "B\n"; }
- |> };
- |>
- |>
- |> main ()
- |> {
- |> B* bptr = new B();
- |> }
- |>
- |>
- |>
- |> The output I get from three different compilers is "A", but I was
- |> expecting to see "B". Does a virtual function not become virtual till
- |> after the constructor is done? What gives? Is it my compilers or is
- |> this a bug in the language definition itself? (Or is it just me? :)
- |>
-
- This is not a bug, it is one aspect of the C++ language specification, which states
- that the member functions (virtual or otherwise) called from within the constructor
- or destructor of a class are those defined in the class or in it's base classes but
- *not* those defined in any derived classes, even if overridden. This is an attempt
- to steer clear of accesses to unconstructed (or recently destructed) parts of the
- derived class. The ARM section 12.7 specifies this.
-
- |> Many thanks,
- |>
- |> Rob DeLine
- |> Computer Science, University of Virginia
- |> deline@Virginia.EDU
-
- All opinions here are my own etc.
- Charles Reindorf
-