Why does C++ refer VTable?

D

DKumar

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?
 
R

Rui Maciel

DKumar said:
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
 
D

dhananjay77

On Friday, December 28, 2012 4:16:14 PM UTC+5:30, DKumar wrote:
Sorry. Added the public keyword now.
 
D

dhananjay77

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.
 
M

Melzzzzz

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?
 
D

DKumar

Compiler: g++
OS: Linux

But this question is generic and not related to any specific compiler or platform.
 
M

Melzzzzz

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.
 
D

DKumar

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?
 
D

DKumar

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.
 
V

Victor Bazarov

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
 
V

Victor Bazarov

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

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
 
M

Melzzzzz

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

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.
 
Ö

Öö Tiib

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top