CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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?

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Do we really need private virtual function?

    Don't forget about protected. Protected functions are accessible by children too.

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Do we really need private virtual function?

    Somehow I can understand protected virtual function but why'd we need private virtual function?
    Quote Originally Posted by ninja9578 View Post
    Don't forget about protected. Protected functions are accessible by children too.

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Do we really need private virtual function?

    Quote Originally Posted by LarryChen View Post
    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

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  7. #7
    Join Date
    Jul 2005
    Posts
    1,030

    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.
    Quote Originally Posted by Paul McKenzie View Post
    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

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    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?
    Quote Originally Posted by monarch_dodra View Post
    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.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Do we really need private virtual function?

    Quote Originally Posted by LarryChen View Post
    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?

  10. #10
    Join Date
    Jul 2005
    Posts
    1,030

    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?
    Quote Originally Posted by Lindley View Post
    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?

  11. #11
    Join Date
    Dec 2009
    Posts
    145

    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

  12. #12
    Join Date
    May 2009
    Posts
    2,413

    Re: Do we really need private virtual function?

    Quote Originally Posted by LarryChen View Post
    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.

  13. #13
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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

  14. #14
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Do we really need private virtual function?

    Quote Originally Posted by LarryChen View Post
    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.

  15. #15
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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
  •  





Click Here to Expand Forum to Full Width

Featured