A question about private virtual function

Discussion in 'C++' started by sun1991, Sep 6, 2006.

  1. sun1991

    sun1991 Guest

    #include <iostream>
    using namespace std;

    class Base{
    public:
    void ToString(){
    ToStringCore();
    }
    private:
    virtual void ToStringCore(){
    cout << "this is Base" << endl;
    }
    };

    class Derived: public Base{};

    int main(){
    Derived d;
    d.ToString();
    }

    -------
    It printed "this is Base", but I can't understand why "d" can call the
    private function of class Base?

    Thanks in advance.
     
    sun1991, Sep 6, 2006
    #1
    1. Advertising

  2. sun1991 wrote:
    > #include <iostream>
    > using namespace std;
    >
    > class Base{
    > public:
    > void ToString(){
    > ToStringCore();
    > }
    > private:
    > virtual void ToStringCore(){
    > cout << "this is Base" << endl;
    > }
    > };
    >
    > class Derived: public Base{};
    >
    > int main(){
    > Derived d;
    > d.ToString();
    > }
    >
    > -------
    > It printed "this is Base", but I can't understand why "d" can call the
    > private function of class Base?


    d didn't call a private member function. d called Base::toString(),
    which is a public member function. Base:toString() called
    Base::ToStringCore(), which it is entitled to do because
    Base::ToString() is a Base member function, and Base member functions
    may call private Base member functions. Private member functions
    wouldn't serve much purpose if they couldn't be called indirectly in
    this way.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 6, 2006
    #2
    1. Advertising

  3. sun1991

    mlimber Guest

    Thomas Tutone wrote:
    > sun1991 wrote:
    > > #include <iostream>
    > > using namespace std;
    > >
    > > class Base{
    > > public:
    > > void ToString(){
    > > ToStringCore();
    > > }
    > > private:
    > > virtual void ToStringCore(){
    > > cout << "this is Base" << endl;
    > > }
    > > };
    > >
    > > class Derived: public Base{};
    > >
    > > int main(){
    > > Derived d;
    > > d.ToString();
    > > }
    > >
    > > -------
    > > It printed "this is Base", but I can't understand why "d" can call the
    > > private function of class Base?

    >
    > d didn't call a private member function. d called Base::toString(),
    > which is a public member function. Base:toString() called
    > Base::ToStringCore(), which it is entitled to do because
    > Base::ToString() is a Base member function, and Base member functions
    > may call private Base member functions. Private member functions
    > wouldn't serve much purpose if they couldn't be called indirectly in
    > this way.


    Compare also the guidelines for access to virtual functions given by
    Guru Sutter:

    http://www.gotw.ca/publications/mill18.htm

    Cheers! --M
     
    mlimber, Sep 6, 2006
    #3
  4. sun1991

    sun1991 Guest

    Well, I should read more. Thanks!
     
    sun1991, Sep 7, 2006
    #4
  5. sun1991

    Default User Guest

    sun1991 wrote:

    > Well, I should read more. Thanks!


    You should quote more too.




    Brian
     
    Default User, Sep 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    812
    Daniel T.
    Feb 4, 2004
  2. IK
    Replies:
    2
    Views:
    621
    hemraj
    Jul 23, 2004
  3. Martin Vorbrodt

    private pure virtual function

    Martin Vorbrodt, Nov 1, 2005, in forum: C++
    Replies:
    10
    Views:
    567
    Kaz Kylheku
    Nov 2, 2005
  4. John Goche
    Replies:
    10
    Views:
    768
    Marcus Kwok
    Dec 8, 2006
  5. Gregor Kofler
    Replies:
    6
    Views:
    218
    Gregor Kofler
    Jun 27, 2008
Loading...

Share This Page