-
June 27th, 2010, 06:10 PM
#1
Do we really need private virtual function?
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?
-
June 27th, 2010, 06:22 PM
#2
Re: Do we really need private virtual function?
Don't forget about protected. Protected functions are accessible by children too.
-
June 27th, 2010, 06:28 PM
#3
Re: Do we really need private virtual function?
Somehow I can understand protected virtual function but why'd we need private virtual function?
Originally Posted by ninja9578
Don't forget about protected. Protected functions are accessible by children too.
-
June 27th, 2010, 06:51 PM
#4
Re: Do we really need private virtual function?
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
June 27th, 2010, 11:46 PM
#5
Re: Do we really need private virtual function?
Originally Posted by LarryChen
Somehow I can understand protected virtual function but why'd we need private virtual function?
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
-
June 28th, 2010, 01:08 AM
#6
Re: Do we really need private virtual function?
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:
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";
}
};
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.
If the entire getName was virtual, and if you are not careful, maybe you can typo "class'" into "class's", and get this:
Code:
This class' name is Base
This class's name is Derived
This is a dumb example, but I hope you get the gist.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
June 28th, 2010, 02:23 PM
#7
Re: Do we really need private virtual function?
Why are these private function virtual? Can we simply make them private non-virtual function to achieve the same purpose? I still could't figure out why we need to make them virtual.
Originally Posted by Paul McKenzie
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
-
June 28th, 2010, 02:24 PM
#8
Re: Do we really need private virtual function?
I don't understand why'd we need to make these virtual functions private. Can we simply make them protected?
Originally Posted by monarch_dodra
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:
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";
}
};
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.
If the entire getName was virtual, and if you are not careful, maybe you can typo "class'" into "class's", and get this:
Code:
This class' name is Base
This class's name is Derived
This is a dumb example, but I hope you get the gist.
-
June 28th, 2010, 02:54 PM
#9
Re: Do we really need private virtual function?
Originally Posted by LarryChen
I don't understand why'd we need to make these virtual functions private. Can we simply make them protected?
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?
-
June 28th, 2010, 03:47 PM
#10
Re: Do we really need private virtual function?
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?
Originally Posted by Lindley
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?
-
June 28th, 2010, 04:34 PM
#11
Re: Do we really need private virtual function?
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
-
June 28th, 2010, 04:58 PM
#12
Re: Do we really need private virtual function?
Originally Posted by LarryChen
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?
The general rule is to always restrict access as much as possible.
In a base class all methods are likely to be public pure virtual but in derived implementation class they're more likely to be private.
-
June 29th, 2010, 05:08 PM
#13
Re: Do we really need private virtual function?
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
-
June 29th, 2010, 05:13 PM
#14
Re: Do we really need private virtual function?
Originally Posted by LarryChen
I don't understand why'd we need to make these virtual functions private. Can we simply make them protected?
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.
-
July 2nd, 2010, 01:35 AM
#15
Re: Do we really need private virtual function?
privatized polymorphism, restricted access and template method.
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|