Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pure virtual function in an ABC

Reply
Thread Tools

Pure virtual function in an ABC

 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-04-2007

Dear mentors and gurus,

I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note
that it is possible to provide a definition for a pure virtual
function, but this usually confuses novices and is best avoided until
later".

I've read through all the later stuff, and (although I may have missed
it) I can't find anything further on this. Can someone please explain
why in all the Halls of Hades you would declare a member function to
be pure virtual (i.e. _must_ be provided by any derived class) and
then provide a definition for it in the Abstract Base Class?!

Thx

 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      07-04-2007
wrote:
> Dear mentors and gurus,
>
> I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note
> that it is possible to provide a definition for a pure virtual
> function, but this usually confuses novices and is best avoided until
> later".
>
> I've read through all the later stuff, and (although I may have missed
> it) I can't find anything further on this. Can someone please explain
> why in all the Halls of Hades you would declare a member function to
> be pure virtual (i.e. _must_ be provided by any derived class) and
> then provide a definition for it in the Abstract Base Class?!


I can thing of only this scenario: Your base class A declares a pure virtual
method, and you have 3 classes X, Y, and Z derived from A. X and Y can implement
the virtual method in the same manner, Z needs a different implementation.
Instead of copying the code for X and Y, you can provide the common
implementation for the pure virtual method in A, so that X and Y can simply
forward the call to the base class version. Z has to provide its own version of
the pure virtual method (were the method not pure, the implementor of Z could
oversee to implement the method, which leads to unforeseen errors). By defining
a pure virtual method you provide a standard implementation, but derived classes
have to state explicitely that this standard implementation is OK.

Regards,
Stuart
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      07-04-2007
wrote:

>
> I've read through all the later stuff, and (although I may have missed
> it) I can't find anything further on this. Can someone please explain
> why in all the Halls of Hades you would declare a member function to
> be pure virtual (i.e. _must_ be provided by any derived class) and
> then provide a definition for it in the Abstract Base Class?!
>
>

Well, in the case of a destructor, because you have to

You may do it because:

1. While the base class function might be a default, you want
the user to explicitly make the decision to use it, in
which case they'll just do:

void Derived:ureVirtualFunction() {
Base:ureVirutalFunction();
}

2. The Base function may provide some features that need to be
done in addition to processing in the derived class function.
 
Reply With Quote
 
gpuchtel
Guest
Posts: n/a
 
      07-04-2007
On Jul 4, 10:12 am, k...@bytebrothers.co.uk wrote:
> Dear mentors and gurus,
>
> I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note
> that it is possible to provide a definition for a pure virtual
> function, but this usually confuses novices and is best avoided until
> later".
>
> I've read through all the later stuff, and (although I may have missed
> it) I can't find anything further on this. Can someone please explain
> why in all the Halls of Hades you would declare a member function to
> be pure virtual (i.e. _must_ be provided by any derived class) and
> then provide a definition for it in the Abstract Base Class?!
>
> Thx


If you want to force derived classes to implement a method, yet
provide default behavior each derived class (method) should call. For
example, the base (abstract) class method might generate a common
unique identifier (base) and the derived class method augments it.

You can "define and invoke a pure virtual method, provided it is
invoked statically and not through the virtual mechanism" (excerpt
from "Inside the C++ Object Model" by Stanley Lippman. p160-161). In
this case, 'get_unique_identifier()' would be declared as a pure
virtual method with a default implementation.

For example:

std::string Concrete_derived::get_unique_identifier()
{
std::string uid = Abstract_base::get_unique_identifier(); // gets
a common prefix

return uid += "unique suffix";
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
x.abc vs x['abc'] Gunter Henriksen Python 1 05-15-2009 07:03 AM
ABC inheriting from ABC vsgdp C++ 1 09-24-2005 08:01 PM
Ain't ($foo eq 'abc') the same as ($foo =~ /^abc$/)? Ha - NOT ON MY BOX!!! usenet@DavidFilmer.com Perl Misc 4 06-15-2005 03:06 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
HttpModule -- how to intercept urls like http://localhost/abc/def or http://localhost/abc/def/ where abc, def are non virtual dir Jiong Feng ASP .Net 0 11-19-2003 05:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57