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

Discussion in 'C++' started by Fred, Jun 3, 2007.

  1. Fred

    Fred Guest

    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.
    Fred, Jun 3, 2007
    #1
    1. Advertising

  2. Re: Why polymorph fails when virtual function is decleared privatein base class and public in derived class?

    * 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?
    Alf P. Steinbach, Jun 3, 2007
    #2
    1. Advertising

  3. Fred

    Ron Natalie Guest

    Re: Why polymorph fails when virtual function is decleared privatein base class and public in derived class?

    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.
    Ron Natalie, Jun 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    786
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    424
    Howard
    May 16, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page