473,799 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about private virtual function

#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.

Sep 6 '06 #1
4 2298

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::ToStringC ore(), 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

Sep 6 '06 #2
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::ToStringC ore(), 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

Sep 6 '06 #3
Well, I should read more. Thanks!

Sep 7 '06 #4
sun1991 wrote:
Well, I should read more. Thanks!
You should quote more too.


Brian
Sep 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2417
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
3
11840
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone use private virtuals?", and the author answered with "almost never, but there are reasons". So, I am asking, what are the reasons. It seems illogical to have one; the point of a virtual function is to be overriden in a derived class, and the...
10
1273
by: p_adib | last post by:
Hi all. In the context of inheritance in c++, I was taught in class that every derived class is "part base class content - part new content". Moreover, I was taught that the constructor of the base class is used to initialize base class content and that the derived class constructor is used to initialize the new contents. Ok, here is where I'm confused. If the derived class is part base content - part new content, then the keyword...
10
4810
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
1
1894
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying values sounds ideal. I would like to know if the usage of this function pointer is valid before I refactor some of the other functions that use the same data structure to complete functions. class Object
13
1698
by: Jess | last post by:
Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{
17
3552
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
5
24822
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.