In my understanding, virtual function should always be public because virtual functions are interface so obviously they should be accessible to the client. Why would we need private virtual function?
Printable View
In my understanding, virtual function should always be public because virtual functions are interface so obviously they should be accessible to the client. Why would we need private virtual function?
Don't forget about protected. Protected functions are accessible by children too.
The reason is that your derived classes are allowed to implement them, but not call them.
The base class is responsible for calling those overridden function in some sequence that the base class decides on. For example, if the sequence of calls must be A(), B(), then C(), and these are virtual, making these functions private ensures that your derived class can't misuse the functions.
Regards,
Paul McKenzie
It can be used to create a "non virtual interface to virtual functions". The idea is that the base class has a bunch of non-virtual functions, and they all call private virtual functions. The derived children overide the private virtuals, but they still call the base non-virtual.
If this is not clear, here is an example:
The advantage of this method is that you can garantee that the derived class only redefines the stuff that needs to change. Everything that imperativelly must not change is left in the non-virtual class.Code:class Base
{
public:
void getName()
{
cout << "This class' name is ";
_getName();
cout << endl;
}
private:
virtual void _getName()
{
cout << "Base";
}
};
class Derived : public Base
{
private:
virtual void _getName()
{
cout << "Derived";
}
};
If the entire getName was virtual, and if you are not careful, maybe you can typo "class'" into "class's", and get this:
This is a dumb example, but I hope you get the gist.Code:This class' name is Base
This class's name is Derived
You never need to make anything private (or protect, for that matter). You could make everything public and the code would still work fine.
However, good design includes restricting access to methods to the smallest scope reasonable given their intended usage. If something is only intended to be called from a non-virtual base class method, then why not make it private?
In my understanding, virtual functions stand for variance and non-functions stand for invariance in general if virtual functions are declared public . I think protected virtual functions stand for mixture of variance and invariance, which means part of invariance is variance. But what is idea behind private virtual function in terms of variance and invariance?
Very an-in-depth-series item to mark!
In terms of polymorphism and accessibility, I think Paul answered your question
In terms of 'variance' and 'invariance', your use of word states it all. It's like a discussion of whether or not friend would violate encapsulation. It offers a new choice of privatized polymorphism
Take a look at this written by Herb Sutter. Your question made be remember this. It was also written 10 years ago so it goes to show that some folks have thought this issue through in the past and this was one conclusion that was reached many years ago by Herb.
http://www.gotw.ca/publications/mill18.htm
Sure you can. Or you can make them private. Do whatever makes sense for your design. In that example I would think that private inheritance doesn't make much sense since the derived class can't call the base class function anyway. in my mind private inheritance makes sense when the base class has no default implementation but you want to force the derived class to provide it. As far as why the function is necessary at all well the article that I posted should explain that. The idea is to separate implementation from interface.
privatized polymorphism, restricted access and template method.