473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Pure virtual function in an ABC


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

Jul 4 '07 #1
3 1616
ke***@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?!
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
Jul 4 '07 #2
ke***@bytebrothers.co.uk 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::PureVirtualFunction() {
Base::PureVirutalFunction();
}

2. The Base function may provide some features that need to be
done in addition to processing in the derived class function.
Jul 4 '07 #3
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";
}

Jul 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
1
by: jinendrashankar | last post by:
Hi All, 1.Please check this code and its output which is working fine while pure virtual functions( FUN1) prototype of Class CA is different then Class CB Function (FUN1). 2. In Class...
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.