Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Why does C++ refer VTable? (http://www.velocityreviews.com/forums/t955893-why-does-c-refer-vtable.html)

DKumar 12-28-2012 10:46 AM

Why does C++ refer VTable?
 
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?

Rui Maciel 12-28-2012 11:08 AM

Re: Why does C++ refer VTable?
 
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

dhananjay77@gmail.com 12-28-2012 12:09 PM

Re: Why does C++ refer VTable?
 
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?



dhananjay77@gmail.com 12-28-2012 12:17 PM

Re: Why does C++ refer VTable?
 
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.

Melzzzzz 12-28-2012 12:37 PM

Re: Why does C++ refer VTable?
 
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?

DKumar 12-28-2012 12:53 PM

Re: Why does C++ refer VTable?
 
Compiler: g++
OS: Linux

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

Melzzzzz 12-28-2012 01:02 PM

Re: Why does C++ refer VTable?
 
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.

DKumar 12-28-2012 01:19 PM

Re: Why does C++ refer VTable?
 
> 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 12-28-2012 05:19 PM

Re: Why does C++ refer VTable?
 
> 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 12-28-2012 05:31 PM

Re: Why does C++ refer VTable?
 
> 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?


All times are GMT. The time now is 01:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.