* 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?
|