Calling a Private function using Virtual Instance

Discussion in 'C++' started by Pravin, Mar 13, 2008.

  1. Pravin

    Pravin Guest

    Hi all,

    please look at this code

    "

    class VirtualBase{

    public:
    virtual int method1() = 0;
    virtual int method2()=0;
    virtual int method3()=0
    };


    class DerivedBase:public VirtualBase
    {
    public:
    int method1(){
    return 2;
    }
    int method2(){
    return 2;
    }
    private:
    int method3(){
    return 2;
    }
    };

    void main(){


    DerivedBase ObjDerivedBase;
    VirtualBase *ObjVirtualBase;

    ObjVirtualBase = new DerivedBase;
    cout<<"\nReturn:"<<ObjVirtualBase->method3();
    }

    "

    Here..inspite of DerivedBase::method3() being private, i can still
    call it using a pointer to VirtualBase after assigning a object of
    DerivedBase.

    I have difficulty understanding how this could be possible?
    Pravin, Mar 13, 2008
    #1
    1. Advertising

  2. Pravin

    utab Guest

    On Mar 13, 1:11 pm, Pravin <> wrote:
    > Hi all,
    >
    > please look at this code
    >
    > "
    >
    > class VirtualBase{
    >
    > public:
    > virtual int method1() = 0;
    > virtual int method2()=0;
    > virtual int method3()=0
    >
    > };
    >
    > class DerivedBase:public VirtualBase
    > {
    > public:
    > int method1(){
    > return 2;
    > }
    > int method2(){
    > return 2;
    > }
    > private:
    > int method3(){
    > return 2;
    > }
    >
    > };
    >
    > void main(){
    >
    > DerivedBase ObjDerivedBase;
    > VirtualBase *ObjVirtualBase;
    >
    > ObjVirtualBase = new DerivedBase;
    > cout<<"\nReturn:"<<ObjVirtualBase->method3();
    >
    > }
    >
    > "
    >
    > Here..inspite of DerivedBase::method3() being private, i can still
    > call it using a pointer to VirtualBase after assigning a object of
    > DerivedBase.
    >
    > I have difficulty understanding how this could be possible?


    I am not sure on the things that I will write, but I got this
    impression out of some tries, the knowledgeable here would make it
    more clear for you, I also wondered the result of this post. When you
    inherit from an abstract base class, the members should be defined in
    the derived classes, but since you can call the private member
    function of a derived class through an abstract base class pointer
    made me to think that since the called function is in the public
    interface of the abstract base class, there is some kind of privilege
    perhaps. I am unaware of this however.
    utab, Mar 13, 2008
    #2
    1. Advertising

  3. Pravin

    Kai-Uwe Bux Guest

    Pravin wrote:

    > Hi all,
    >
    > please look at this code
    >
    > "
    >
    > class VirtualBase{
    >
    > public:
    > virtual int method1() = 0;
    > virtual int method2()=0;
    > virtual int method3()=0
    > };
    >
    >
    > class DerivedBase:public VirtualBase
    > {
    > public:
    > int method1(){
    > return 2;
    > }
    > int method2(){
    > return 2;
    > }
    > private:
    > int method3(){
    > return 2;
    > }
    > };
    >
    > void main(){
    >
    >
    > DerivedBase ObjDerivedBase;
    > VirtualBase *ObjVirtualBase;
    >
    > ObjVirtualBase = new DerivedBase;
    > cout<<"\nReturn:"<<ObjVirtualBase->method3();
    > }
    >
    > "
    >
    > Here..inspite of DerivedBase::method3() being private, i can still
    > call it using a pointer to VirtualBase after assigning a object of
    > DerivedBase.
    >
    > I have difficulty understanding how this could be possible?


    Access (public, private, protected) only relates to the static types. Since
    *ObjVirtualBase is of static type VirtualBase, it allows you to access the
    public member function method3. The dynamic type only determines which
    implementation will be dispatched; it does not determine whether the member
    function is accessible. See [11.6/1] for details.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 13, 2008
    #3
  4. Pravin

    red floyd Guest

    Pravin wrote:
    >[redacted]
    > void main(){
    >
    >
    > DerivedBase ObjDerivedBase;
    > VirtualBase *ObjVirtualBase;
    >
    > ObjVirtualBase = new DerivedBase;
    > cout<<"\nReturn:"<<ObjVirtualBase->method3();
    > }


    Totally unrelated to your question, but I'm guessing you're just learning.

    Your code is ill-formed, and should generate a diagnostic out of your
    compiler.

    main returns int. Period. Nothing else.
    red floyd, Mar 14, 2008
    #4
    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:
    766
    Daniel T.
    Feb 4, 2004
  2. Andreas Lagemann
    Replies:
    8
    Views:
    452
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    423
  4. Replies:
    11
    Views:
    675
    James Kanze
    Sep 10, 2006
  5. John Goche
    Replies:
    10
    Views:
    715
    Marcus Kwok
    Dec 8, 2006
Loading...

Share This Page