Virtual function access level in derived class

Discussion in 'C++' started by cppquester, Apr 12, 2010.

  1. cppquester

    cppquester Guest

    I figured that a private virtual function is called through the base
    class when the function is declared there public.
    I think this is fine, but it was compiled without warning.
    I use gcc, is this behaviour guaranteed in the standard?

    Thanks,
    Marc
    cppquester, Apr 12, 2010
    #1
    1. Advertising

  2. cppquester

    Kai-Uwe Bux Guest

    cppquester wrote:

    > I figured that a private virtual function is called through the base
    > class when the function is declared there public.
    > I think this is fine, but it was compiled without warning.
    > I use gcc, is this behaviour guaranteed in the standard?


    Yes, see [11.6/1]:

    The access rules (clause 11) for a virtual function are determined by its
    declaration and are not affected by the rules for a function that later
    overrides it. [Example:

    class B {
    public:
    virtual int f();
    };

    class D : public B {
    private:
    int f();
    };

    void f()
    {
    D d;
    B* pb = &d;
    D* pd = &d;
    pb->f(); //OK: B::f() is public,
    // D::f() is invoked
    pd->f(); //error: D::f() is private
    }
    ?end example] ...


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Apr 12, 2010
    #2
    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. Xiangliang Meng
    Replies:
    2
    Views:
    408
    Jack Klein
    Jun 21, 2004
  2. pabbu
    Replies:
    8
    Views:
    720
    Marc Boyer
    Nov 7, 2005
  3. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
  5. Alex Shen
    Replies:
    5
    Views:
    418
    Alex Shen
    Oct 30, 2010
Loading...

Share This Page