Overriding Virtual Function clarification...

Discussion in 'C++' started by Achintya, Jul 23, 2005.

  1. Achintya

    Achintya Guest

    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    #include<iostream>
    using namespace std;

    class A
    {
    int i;
    virtual void f(){ cout<<endl<<"i= "<<i; }
    public:
    A(int j) { i = j; }
    };

    class B : public A
    {
    int j;
    virtual void g() { cout<<endl<<"j= "<<j; }
    public:
    virtual void f(){ cout<<"I'm in B::f()"; }
    B(int k):A(k+10){ j = k; }

    };

    int main()
    {
    A* a_base;
    B* b_derived;

    a_base = new B(1);
    a_base->f();
    }
    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    Hi,

    In the above code the virtual function declaration in A is causing a
    compiler error since its private. Does this mean that the virtual
    function overring requires the base delaration also to be public.
    Kindly explain...as i hadn't tried this earlier.

    -praveen.
     
    Achintya, Jul 23, 2005
    #1
    1. Advertising

  2. >
    > In the above code the virtual function declaration in A is causing a
    > compiler error since its private. Does this mean that the virtual
    > function overring requires the base delaration also to be public.
    > Kindly explain...as i hadn't tried this earlier.
    >
    > -praveen.
    >


    Private base class member functions are not accessable to derived
    classes, try to make them protected instead.

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
     
    Tobias Blomkvist, Jul 23, 2005
    #2
    1. Advertising

  3. Achintya

    Achintya Guest

    Tobias Blomkvist wrote:
    > >
    > > In the above code the virtual function declaration in A is causing a
    > > compiler error since its private. Does this mean that the virtual
    > > function overring requires the base delaration also to be public.
    > > Kindly explain...as i hadn't tried this earlier.
    > >
    > > -praveen.
    > >

    >
    > Private base class member functions are not accessable to derived
    > classes, try to make them protected instead.
    >
    > Tobias


    Hi,

    Thanks..Thats fine, I tried that before posting here and was
    successful. but I am interested in reason behind...

    -praveen
    > --
    > IMPORTANT: The contents of this email and attachments are confidential
    > and may be subject to legal privilege and/or protected by copyright.
    > Copying or communicating any part of it to others is prohibited and may
    > be unlawful.
     
    Achintya, Jul 23, 2005
    #3
  4. Achintya wrote in news:1122128095.067742.271170
    @g43g2000cwa.googlegroups.com in comp.lang.c++:

    > int main()
    > {
    > A* a_base;
    > B* b_derived;
    >
    > a_base = new B(1);
    > a_base->f();
    > }
    > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    >
    > Hi,
    >
    > In the above code the virtual function declaration in A is causing a
    > compiler error since its private. Does this mean that the virtual
    > function overring requires the base delaration also to be public.
    > Kindly explain...as i hadn't tried this earlier.
    >


    No, public is required as you call A::f() from main(), i.e. a
    context where public access to A::f() is required.

    When you make a call to A::f() the compiler doesn't care
    wether A::f() is virtual, or if its been overriden, it only
    checks the accessability of A::f().

    The fact that in this case a_base actually points to a B with
    an accessable f() doesen't matter. Type and access checking is
    done by the compiler at compile time, not at runtime, and in
    general the compiler doesn't know the dynamic type (B - above)
    that a pointer (A *a_base - above) points too.

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jul 23, 2005
    #4
  5. > Hi,
    >
    > Thanks..Thats fine, I tried that before posting here and was
    > successful. but I am interested in reason behind...
    >
    > -praveen
    >


    Everything declared private is supposed to be private, unaccessible to
    others. Protected means that it is still shielded from outside access,
    but accessible to derived classes. These are design options provided to
    separate intentional functionality, thus making the interface clearer.

    Additionally you can affect the inherited access level by deriving
    with public, protected and private:

    class x1 : public y {};
    class x2 : protected y {};
    class x3 : private y {};

    Private is the default base class access specifier:

    // private access, no of y's functions will be accessible to x4
    class x4 : y {};

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
     
    Tobias Blomkvist, Jul 23, 2005
    #5
  6. > #include<iostream>
    > using namespace std;
    >
    > class A
    > {
    > int i;
    > virtual void f(){ cout<<endl<<"i= "<<i; }
    > public:
    > A(int j) { i = j; }
    > };
    >
    > class B : public A
    > {
    > int j;
    > virtual void g() { cout<<endl<<"j= "<<j; }
    > public:
    > virtual void f(){ cout<<"I'm in B::f()"; }
    > B(int k):A(k+10){ j = k; }
    >
    > };
    >


    The problem is, you are trying to change the access specifier of Base class
    private function to public and trying to access it from mail using
    polymorphism.
    In C++, we can not increase the access of a variable/function from lower
    level to upper level (example from private to public)

    But, if your base class f() was public, then you can change your derived
    class overloaded f()'s access specifier as private.

    Now when you call your Derived function f() using base pointer, it does not
    give you any error.

    Regards
    Girish
     
    Girish Shetty, Jul 25, 2005
    #6
  7. * Tobias Blomkvist:
    > >
    > > In the above code the virtual function declaration in A is causing a
    > > compiler error since its private. Does this mean that the virtual
    > > function overring requires the base delaration also to be public.
    > > Kindly explain...as i hadn't tried this earlier.
    > >
    > > -praveen.
    > >

    >
    > Private base class member functions are not accessable to derived
    > classes, try to make them protected instead.


    Private virtual member functions can be overridden.

    The problem in the OP's code is not that, but that he's trying to access a
    member function through a pointer to a class where that 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, Jul 25, 2005
    #7
  8. * Girish Shetty:
    > > #include<iostream>
    > > using namespace std;
    > >
    > > class A
    > > {
    > > int i;
    > > virtual void f(){ cout<<endl<<"i= "<<i; }
    > > public:
    > > A(int j) { i = j; }
    > > };
    > >
    > > class B : public A
    > > {
    > > int j;
    > > virtual void g() { cout<<endl<<"j= "<<j; }
    > > public:
    > > virtual void f(){ cout<<"I'm in B::f()"; }
    > > B(int k):A(k+10){ j = k; }
    > >
    > > };
    > >

    >
    > The problem is, you are trying to change the access specifier of Base class
    > private function to public and trying to access it from mail using
    > polymorphism.
    > In C++, we can not increase the access of a variable/function from lower
    > level to upper level


    Sorry, that's incorrect.


    > (example from private to public)


    Right, the function must be accessible to do anything except overriding it.

    Although, if the private function is virtual it can be overridden by a
    forwarder function, which you can give any access specifier you want.

    --
    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, Jul 25, 2005
    #8
  9. * Alf P. Steinbach:
    >
    > Although, if the private function is virtual it can be overridden by a
    > forwarder function, which you can give any access specifier you want.


    Strike that. It can be overridden but of course not forwarded to. *banging
    head against keyboard, consuming several litres of coffee to wake up*

    --
    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, Jul 25, 2005
    #9
  10. Achintya

    Achintya Guest

    Hi,

    Thanks all for dicussing this topic. Now I can reason out the cause.

    Anybody can suggest good resource on net on how VTABLEs work?

    -vs_p...

    Alf P. Steinbach wrote:
    > * Alf P. Steinbach:
    > >
    > > Although, if the private function is virtual it can be overridden by a
    > > forwarder function, which you can give any access specifier you want.

    >
    > Strike that. It can be overridden but of course not forwarded to. *banging
    > head against keyboard, consuming several litres of coffee to wake up*
    >
    > --
    > 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?
     
    Achintya, Jul 25, 2005
    #10
  11. Achintya

    Achintya Guest

    Hi,

    Thanks all for dicussing this topic. Now I can reason out the cause.

    Anybody can suggest good resource on net on how VTABLEs work?

    -vs_p...

    Alf P. Steinbach wrote:
    > * Alf P. Steinbach:
    > >
    > > Although, if the private function is virtual it can be overridden by a
    > > forwarder function, which you can give any access specifier you want.

    >
    > Strike that. It can be overridden but of course not forwarded to. *banging
    > head against keyboard, consuming several litres of coffee to wake up*
    >
    > --
    > 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?
     
    Achintya, Jul 25, 2005
    #11
  12. Achintya

    vindhya Guest

    vindhya, Jul 25, 2005
    #12
    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. CoolPint
    Replies:
    3
    Views:
    386
    Ron Natalie
    Jan 26, 2004
  2. james
    Replies:
    11
    Views:
    2,352
    red floyd
    Feb 6, 2004
  3. ctor
    Replies:
    13
    Views:
    586
    Howard
    Mar 9, 2006
  4. a
    Replies:
    7
    Views:
    364
    dasjotre
    Jun 28, 2007
  5. Peter Davis
    Replies:
    3
    Views:
    959
    Peter Davis
    Jul 25, 2012
Loading...

Share This Page