home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: Calling pure virtual functions in base class constructor
- Message-ID: <1992Nov10.010626.26528@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <720990361snx@trmphrst.demon.co.uk>
- Date: Tue, 10 Nov 1992 01:06:26 GMT
- Lines: 68
-
- In article <720990361snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk writes:
- >In article <miLRVB10w165w@csource.oz.au> david@csource.oz.au writes:
- >> I'm somewhat puzzled as to why the current rule on calling pure virtual
- >> functions from a base class does not simply prohibit them from being
- >> there (ie. by a compiler error) rather than defining the results as
- >> being 'undefined'. (ARM sec 12.7)
- >The reason is that it is quite legal to have a definition of a pure
- >virtual function, thus ...
- >
- >class A
- > {
- > // ...
- > ~A() { purefunc(); }
- > virtual void purefunc() = 0;
- > };
- >
- >void A::purefunc()
- >{
- > cout << "You called a pure virtual function !\n";
- >}
- >
- >This facility might be useful when you want your abstract base class to
- >provide some kind of default behaviour, but you want to force derived
- >classes to add additional behaviour (or, at least, hint strongly :-).
-
- Not quite. It does not supply 'default behaviour', but
- is called in two by a statically, i.e. like
-
- A::purefunc(); // from inside a member
- a.purefunc(); // from outside the object
-
- >
- >Actually, it occurrs to me that it should be possible to do this yourself,
- >without adding to code size (probably) - just declare your virtual
- >functions ...
- >
- >class A
- > {
- > // ...
- > ~A() { purefunc(); }
- > virtual void purefunc() = 0;
- > };
- >
- >inline void A::purefunc() { assert(0); }
-
- No, this cannot work. The body of a pure virtual cannot be
- called though a pointer or reference, only from a 'value'. Had
- the pure specifier not been included, the body would have been
- called virtually during construction of the A base only.
- >
- >Pure virtual functions can only be called explicitly, under circumstances
- >where the actual type can be deduced at compile time, which are the
- >conditions allowing inline virtuals to be expanded inline. Then again, I
- >could be wrong.
-
- IMHO you are wrong. It is not a matter of deduction, the programmer
- must explicitly override the virtual call mechanism. Virtual calls
- (indirections via the vtble) can be replaced by direct calls only
- in code outside the object where the compiler can deduce the actual
- object type, this cannot ever be the case inside a member function
- as far as I can see (because no member can know what the most derived
- object is).
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-