473,662 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why polymorph fails when virtual function is decleared private in base class and public in derived class?

I've got the following code:

#include <iostream>
class Base{
private:
virtual void f(int) { std::cout << "f in Base" << std::endl; }
};

class Derived : public Base{
public:
virtual void f(int) { std::cout << "f in Derived" << std::endl; }
};
int main(void)
{
Base base;
Derived derived;

Base* p = &derived;
p->f(2);

return 1;
}

In base class f is decleared as private member function, and the
compiler complains that pointer p couldn't access private member in
base. It's clear that polymorph fails here. Why does it happen? Thanks
for help.

Jun 3 '07 #1
2 1491
* Fred:
I've got the following code:

#include <iostream>
class Base{
private:
virtual void f(int) { std::cout << "f in Base" << std::endl; }
};

class Derived : public Base{
public:
virtual void f(int) { std::cout << "f in Derived" << std::endl; }
};
int main(void)
{
Base base;
Derived derived;

Base* p = &derived;
p->f(2);

return 1;
}

In base class f is decleared as private member function, and the
compiler complains that pointer p couldn't access private member in
base.
Right.

It's clear that polymorph fails here.
Polymorphism doesn't fail.

Why does it happen?
The statically known type (the type known at compile time) is Base. And
in Base, that member function is private.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 3 '07 #2
The dynamic type of the object is only tested at the last step
in the selection of the virtual function. Everything else uses
the static type. The call is done roughly as follows:

1. The name of the function is looked up in the calling scope.
2. Overloads for that name are checked to find the best match.
3. The protection for that overload is checked.
4. Then IF the function is virtual, the final overriding function
is called

The difference between static and dynamic type only comes into play
at step 4.
Jun 3 '07 #3

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

Similar topics

19
2332
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
7
1803
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number; virtual long myfunc(void); }
9
1453
by: Mika Vainio | last post by:
hi everybody, i'm working on the following problem: i need to build a 26-nary tree to save a data dictionary. every letter of the words is represented by a cell (cell has a pointer-vector cell* children). the last letter is a node (node:cell, additional property "pagenr"). now: i understood the principals of polymorph objects and it worked fine with the ususal examples. but in my case it does not! there is no compilation or linking error...
20
2404
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();}
13
2835
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
11
3424
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
10
4786
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
4194
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
2075
by: dragoncoder | last post by:
Hello experts, I was just playing around wrote this code. sundev1:/home/ptiwary/rnd $ cat a1.cpp #include <iostream> using namespace std; class Base
0
8432
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
8856
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...
1
8545
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
8633
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...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
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.