Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why polymorph fails when virtual function is decleared private in base class and public in derived class?

Reply
Thread Tools

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

 
 
Fred
Guest
Posts: n/a
 
      06-03-2007
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-03-2007
* 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?
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      06-03-2007
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.
 
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
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments