Why does C++ refer VTable?

Discussion in 'C++' started by DKumar, Dec 28, 2012.

  1. DKumar

    DKumar Guest

    class A{
    virtual void f1(){}
    };

    class B : public A{
    void f1(){}
    };

    int main()
    {
    A *pa = new B;
    pa->f1();
    }

    In the above case, why compiler doesn't know which f1() I am trying to call? It has the address of B, so obviously it has to call B's f1(). Why virtual table needed here to know which f1 needs to be called?
     
    DKumar, Dec 28, 2012
    #1
    1. Advertising

  2. DKumar

    Rui Maciel Guest

    DKumar wrote:

    > class A{
    > virtual void f1(){}
    > };
    >
    > class B : public A{
    > void f1(){}
    > };
    >
    > int main()
    > {
    > A *pa = new B;
    > pa->f1();
    > }
    >
    > In the above case, why compiler doesn't know which f1() I am trying to
    > call?


    There is no ambiguity. There is, however, a problem. You are trying to
    call a private member function, which is a no-no.


    Rui Maciel
     
    Rui Maciel, Dec 28, 2012
    #2
    1. Advertising

  3. DKumar

    Guest

    On Friday, December 28, 2012 4:16:14 PM UTC+5:30, DKumar wrote:
    Sorry. Added the public keyword now.
    > class A{
    > public:
    > virtual void f1(){}
    >
    > };
    >
    >
    >
    > class B : public A{
    > public:
    > void f1(){}
    >
    > };
    >
    >
    >
    > int main()
    >
    > {
    >
    > A *pa = new B;
    >
    > pa->f1();
    >
    > }
    >
    >
    >
    > In the above case, why compiler doesn't know which f1() I am trying to call? It has the address of B, so obviously it has to call B's f1(). Why virtual table needed here to know which f1 needs to be called?
     
    , Dec 28, 2012
    #3
  4. DKumar

    Guest

    A *pa = new B;

    or
    B b;
    A *pa = &b;

    This means A will address of B, then why does it need to refer vtable to actually know which function needs to be called?

    And this function f1() is a public one. I missed to write access specifier.
     
    , Dec 28, 2012
    #4
  5. DKumar

    Melzzzzz Guest

    On Fri, 28 Dec 2012 04:17:06 -0800, dhananjay77 wrote:

    > A *pa = new B;
    >
    > or B b;
    > A *pa = &b;
    >
    > This means A will address of B, then why does it need to refer vtable to
    > actually know which function needs to be called?
    >
    > And this function f1() is a public one. I missed to write access
    > specifier.


    What compiler, what platform?
     
    Melzzzzz, Dec 28, 2012
    #5
  6. DKumar

    DKumar Guest

    Compiler: g++
    OS: Linux

    But this question is generic and not related to any specific compiler or platform.
     
    DKumar, Dec 28, 2012
    #6
  7. DKumar

    Melzzzzz Guest

    On Fri, 28 Dec 2012 04:53:27 -0800, DKumar wrote:

    > Compiler: g++
    > OS: Linux
    >
    > But this question is generic and not related to any specific compiler or
    > platform.


    Hm, g++ 4.7.2 (-O2) completely optimizes out function call.
     
    Melzzzzz, Dec 28, 2012
    #7
  8. DKumar

    DKumar Guest

    > Hm, g++ 4.7.2 (-O2) completely optimizes out function call.

    Could you give any use case where it definitely has to refer vtable to identify the function call?
     
    DKumar, Dec 28, 2012
    #8
  9. DKumar

    DKumar Guest

    > A *pa = (argc>2? new B: new A);
    > return pa->f1();



    Even in this case once pa is assigned with either B or A, it can refer to respective f1() from their respective class without referring to vtable.
     
    DKumar, Dec 28, 2012
    #9
  10. DKumar

    DKumar Guest

    > When compiler can't determine exact type, eg return object from some
    > extern function or call from extern function passing base class object.
    > Or make choice of several derived objects based on some conditionwhich
    > changes. Use imagination ;)


    -------------------

    I agree that sometimes compiler can't determine which object it will point to, but what I am saying is that once it is passed or assigned to base pointer, base pointer should know which function to call and should call it without using vtable. why is the vtable being used just to call which functionto call?
     
    DKumar, Dec 28, 2012
    #10
  11. On 12/28/2012 12:19 PM, DKumar wrote:
    >> A *pa = (argc>2? new B: new A);
    >> return pa->f1();

    >
    >
    > Even in this case once pa is assigned with either B or A, it can
    > refer to respective f1() from their respective class without
    > referring to vtable.


    FCOL, what do you mean by that? Do you mean to point out that the
    compiler *could* generate the code analogous to

    A *pa = (argc > 2? new B : new A);
    return (argc > 2
    ? invoke<B::f1>(static_cast<B*>(pa))
    : invoke<A::f1>(pa));

    given some hypothetical implementation of 'invoke' template that does
    not involve using the virtual function mechanism? To that I say, a well
    written optimizing compiler probably does that already. The original
    example was supposed to illustrate a situation where the compiler
    *cannot* make such distinction (A vs B) easily. For instance, when 'pa'
    is initialized from the return value of a function that is unknown to
    the compiler at the time of compilation of this module.

    If it's unknown what derived class 'pa' represents, then the "vtable" is
    a way to ensure the correct member function is called. Isn't that what
    everybody has been trying to say? Perhaps it's not what you were
    asking, then consider clarifying what exactly you want to know.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 28, 2012
    #11
  12. On 12/28/2012 12:31 PM, DKumar wrote:
    >> When compiler can't determine exact type, eg return object from some
    >> extern function or call from extern function passing base class object.
    >> Or make choice of several derived objects based on some conditionwhich
    >> changes. Use imagination ;)

    >
    > -------------------
    >
    > I agree that sometimes compiler can't determine which object it will
    > point to, but what I am saying is that once it is passed or assigned
    > to base pointer, base pointer should know which function to call and
    > should call it without using vtable.


    HOW?

    > why is the vtable being used
    > just to call which function to call?


    Because that is at this point the widely accepted way of implementing
    polymorphic invocation of virtual functions. Simple, efficient.
    Others, if exist, are probably less efficient, or much more complicated
    (cumbersome) to implement, and thus aren't in use.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 28, 2012
    #12
  13. DKumar

    Melzzzzz Guest

    On Fri, 28 Dec 2012 09:31:06 -0800 (PST)
    DKumar <> wrote:

    > > When compiler can't determine exact type, eg return object from
    > > some extern function or call from extern function passing base
    > > class object. Or make choice of several derived objects based on
    > > some conditionwhich changes. Use imagination ;)

    >
    > -------------------
    >
    > I agree that sometimes compiler can't determine which object it will
    > point to, but what I am saying is that once it is passed or assigned
    > to base pointer, base pointer should know which function to call and
    > should call it without using vtable. why is the vtable being used
    > just to call which function to call?


    Because vtable is all compiler gets when getting base class object
    through pointer/reference.
    vtable is a one of ways to know which function to call.
    There are others but less efficient or more involving.
    Eg small Eiffel compiler didn;t use vtable rather trees
    of tests. That had drawback that all source code must be
    visible.
     
    Melzzzzz, Dec 28, 2012
    #13
  14. DKumar

    Öö Tiib Guest

    On Friday, 28 December 2012 19:19:55 UTC+2, DKumar wrote:
    > > A *pa = (argc>2? new B: new A);
    > > return pa->f1();

    >
    > Even in this case once pa is assigned with either B or A, it can
    > refer to respective f1() from their respective class without
    > referring to vtable.


    The vtable pointer *is* "the knowledge" of A subobject what class it
    belongs actually into. It has no other information about that.

    That is just the common way how compilers implement it.
    There are myriad of ways how to hold some sort of "knowledge".
    If you have some more efficient way then propose it to compiler
    implementers or write one yourself.
     
    Öö Tiib, Dec 29, 2012
    #14
    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. Pani
    Replies:
    2
    Views:
    363
    Alexander Terekhov
    Jul 23, 2003
  2. Daniel Heiserer
    Replies:
    1
    Views:
    2,913
    Artie Gold
    Aug 1, 2003
  3. Clint Ruen
    Replies:
    4
    Views:
    547
    Richard Herring
    Dec 7, 2004
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    912
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,003
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page