473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 1469
* 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
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
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;...
9
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*...
20
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
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
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...
10
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...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.