I just spotted this in some code:
class Foo {
[...]
private:
virtual void Bar() = 0;
[...]
}
Does this have any purpose?
(I am trying to port some code from VS to G++, and this caught my attention)
I just spotted this in some code:
class Foo {
[...]
private:
virtual void Bar() = 0;
[...]
}
Does this have any purpose?
(I am trying to port some code from VS to G++, and this caught my attention)
This is a pure virtual function that happens to be private. This makes it so that a derived class must implement the method. In this case Bar.
I think you may be confused b/c this is done to create "interfaces" in C++ and a lot of times people think of these as public. There are cases where you may want to define an interface that is private where a public method uses those private methods in order to ensure the order of how they are called. (I believe this is called the Template Method)
For a relatively bad example :)
I'm going to quote a brief explanation from the great C++ FAQ Lite which sums it up well:
The C++ FAQ Lite was updated in the meantime:
The usual "academic" answer is: access specifiers and virtuality are orthogonal - one does not affect the other.
A little more practical answer: private virtual functions are often used to implement the Template Method design pattern. In languages that do not support private virtual functions, the template method needs to be public although it is not really meant to be a part of the interface.
ISO C++ 2003 explicitly allows it:
§10.3 states nothing about access specifier and contains even a footnote in the second clause stating in the context of virtual function overrides:
The code is fully legal.
The only purpose it seems to serve is to provide a common interface.
BTW even though a function is declared as private virtual, it can still be implemented and called with the class instance or from friends.
Nonetheless, this sort of things usually meant to serve as interface, yet I don't do it this way.
It makes the function pure virtual as opposed to virtual.
No implementation is provided by default and the intent is that the function implementation must be specified by an inheriting class. This can be overriden however.
You sometimes see complete classes where all member functions are specified as pure virtual in this manner.
These are Abstract Base Classes, sometimes referred to as Interface Classes, and the designer of the ABC is saying to you, "I have now idea how this functionality would be implemented for all specialisations of this base class. But, you must have all of these defined for your specialisation to work and you know how your object should behave".
Edit: Oops, just spotted the fact that the member pure virtual function is private. (Thanks Michael) This changes things slightly.
When this base class is inherited using private inheritance it changes things. Basically what the designer of the base class is doing is saying is that, when your derived class calls a non-private function in the base class. part of the behaviour has been delegated to your specialisation of the function in your derived class. The non-private member is doing "something" and part of that "something" is a call, via the pure virtual base class function, to your implementation.
So some public function in Foo is calling the Bar function inside Foo, and it is relying on the fact that you will provide a specialised implementation of the Bar function for your particular case.
Scott Meyers refers to this as "implemented in terms of".
BTW Just chuckling about the number of answers that were quickly deleted by people who also didn't see the "fine print" in the question! (-:
HTH
cheers,