Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A question about private virtual function

Reply
Thread Tools

A question about private virtual function

 
 
sun1991
Guest
Posts: n/a
 
      09-06-2006
#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.

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-06-2006

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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      09-06-2006
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

 
Reply With Quote
 
sun1991
Guest
Posts: n/a
 
      09-07-2006
Well, I should read more. Thanks!

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-07-2006
sun1991 wrote:

> Well, I should read more. Thanks!


You should quote more too.




Brian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments