Topic: Is there any rational for a body of a pure virtual funct


Author: kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425)
Date: 23 Sep 1994 11:05:26 GMT
Raw View
In article <35st3v$g65@darkstar.UCSC.EDU> panzer@cse.ucsc.edu (John
Panzer) writes:

|> In article <KANZE.94Sep22202705@slsvhdt.us-es.sel.de> kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425) writes:
|> >In article <35q9pg$2h8@darkstar.UCSC.EDU> panzer@cse.ucsc.edu (John
|> >Panzer) writes:

|> >|> In article <KANZE.94Sep21130449@slsvhdt.us-es.sel.de> kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425) writes:
|> ...

|> >|> In other words, when constructing b, first A::A is called and then
|> >|> B::B is called.  While A::A is running, the "B" part of b hasn't
|> >|> been constructed yet, and this includes virtual functions.  Thus
|> >|> you end up calling a pure virtual function without using explicit
|> >|> scoping.
|> >
|> >Not quite.  You end up calling a non-existing function.  The results
|> >are undefined.

|> Well, that's what I meant in this paragraph.  But my argument was
|> that if I _had_ provided a body for A::foo, it would have been
|> called when b was constructed.  Taking a look at the ARM, this
|> appears not to be the case (sec 12.7).

|> The ARM states that the only way you can call a defined pure virtual
|> is via explicit qualification.  When I actually try it, however,
|> I get the behavior I described above (calls in the context of
|> a constructor or destructor obey the first paragraph of sec 12.7,
|> notwithstanding the last paragraph).  Has there been any change
|> in the standard on this?  Or is this just an accidental behavior?

The only way you can legally call a defined pure virtual function is
with explicite qualification, but...  calling a pure virtual function
in a constructor or destructor is undefined behavior.  The compiler
can generate code to do just about anything it wishes.  Good compilers
will generally call a function which outputs an error message and
aborts, but the compiler might also format your disk, or ... call the
defined function.
--
James Kanze      Tel.: (+33) 88 14 49 00     email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung






Author: rmartin@rcmcon.com (Robert Martin)
Date: Fri, 23 Sep 1994 16:11:23 GMT
Raw View
panzer@cse.ucsc.edu (John Panzer) writes:

>if I _had_ provided a body for A::foo, [a pure virtual function
>called in the constructor of A] it would have been called when b [a
>class derived from A] was constructed.  Taking a look at the ARM,
>this appears not to be the case (sec 12.7).

>The ARM states that the only way you can call a defined pure virtual
>is via explicit qualification.  When I actually try it, however,
>I get the behavior I described above

I would say that your compiler has a bug.  The vtbl for A will have a
slot for 'foo' that *should not* point at A::foo.  Instead it should
point to some function that pops up a dialog or prints a message
saying "pure virtual function called" or something like that.
--
Robert Martin       | Design Consulting   | Training courses offered:
Object Mentor Assoc.| rmartin@rcmcon.com  |   Object Oriented Analysis
2080 Cranbrook Rd.  | Tel: (708) 918-1004 |   Object Oriented Design
Green Oaks IL 60048 | Fax: (708) 918-1023 |   C++




Author: panzer@cse.ucsc.edu (John Panzer)
Date: 22 Sep 1994 21:31:43 GMT
Raw View
In article <KANZE.94Sep22202705@slsvhdt.us-es.sel.de> kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425) writes:
>In article <35q9pg$2h8@darkstar.UCSC.EDU> panzer@cse.ucsc.edu (John
>Panzer) writes:
>
>|> In article <KANZE.94Sep21130449@slsvhdt.us-es.sel.de> kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425) writes:
...
>
>|> There are other special cases.  For instance, if you call a member
>|> function from within a constructor or destructor, you get the version
>|> of that function which is applicable to the current class.  Ex:
>
>|> class A {
>|> public:
>|>   A() {foo();}
>|>   virtual void foo()=0; // A pure virtual function.
>|> };
>
>|> class B : public A {
>|> public:
>|>   B() {}
>|>   void foo() {cout<<"foo";}
>|> }
>
>|> B b; // Generates runtime error (we hope), or compiler warning (we wish)
>
>|> In other words, when constructing b, first A::A is called and then
>|> B::B is called.  While A::A is running, the "B" part of b hasn't
>|> been constructed yet, and this includes virtual functions.  Thus
>|> you end up calling a pure virtual function without using explicit
>|> scoping.
>
>Not quite.  You end up calling a non-existing function.  The results
>are undefined.

Well, that's what I meant in this paragraph.  But my argument was
that if I _had_ provided a body for A::foo, it would have been
called when b was constructed.  Taking a look at the ARM, this
appears not to be the case (sec 12.7).

The ARM states that the only way you can call a defined pure virtual
is via explicit qualification.  When I actually try it, however,
I get the behavior I described above (calls in the context of
a constructor or destructor obey the first paragraph of sec 12.7,
notwithstanding the last paragraph).  Has there been any change
in the standard on this?  Or is this just an accidental behavior?

John Panzer