0

I was following this tutorial, trying to understand virtual table and the whole process behind pointer and virtual functions in C++.

Not sure, when I have code like this:

D1 d1;
Base *dPtr = &d1;
dPtr->function1();

Why do I need all of this virtual table management? Why compiler simply don't assign the memory address of d1 (or base, if there aren't any) overridden virtual function?

I mean: it could elaborate there at compile time if it needs the D1 functon1() address or Base functon1() address. It know at that time. Why lose time and resources later at runtime looking on virtual tables?

I miss this point. Fancy example?

NH.
  • 2,240
  • 2
  • 23
  • 37
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • The compiler will do this if it can prove that it knows what function you will call. – NathanOliver Mar 05 '18 at 15:35
  • The compiler might be able to detect this situation and optimize out the lookup. You would have to check yourself for your compiler and your situation. – François Andrieux Mar 05 '18 at 15:36
  • There is no mention of vtable in the standard. It's purely a helper concept that is commonly used because it's been proven to be reasonably efficient and no one has anything better. – UKMonkey Mar 05 '18 at 15:37
  • The fancy example would be when the type of the object of `dPtr` is pointing to is not so easy to determine at compile time (e.g. when `dPtr` was returned from a function or retrieved from a data structure). In that case, the runtime needs some way to query the object to determine which virtual method is appropriate to execute; the vtable pointer and vtable is that mechanism. – Jeremy Friesner Mar 05 '18 at 15:38

1 Answers1

5

This is my function:

void foo(Base *pBase) {
  pBase->function1();
}

I compile it in isolation and give you an object file with a header. Months before you even dream up of D1. How would the compiler "use the address of D1's function1 directly" here?

It can't. That's why some form of indirection is required.

Beyond that, a virtual function table isn't required in the sense that every C++ implementation would use one. It's just the most popular implementation technique employed by compilers today.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • 1
    `delete(new ThingCaller()->callFoo(myD1))` nope... still needs the indirection... curses. – UKMonkey Mar 05 '18 at 15:39
  • I think in the (near?) future people will read this; and wonder more about the upvote on that than the comment ;) – UKMonkey Mar 05 '18 at 15:41
  • @UKMonkey groan. – Quentin Mar 05 '18 at 15:42
  • 1
    @Quentin - Careful, you are on the verge of joining us in eternal anguish at this meme :) – StoryTeller - Unslander Monica Mar 05 '18 at 15:43
  • 2
    I want nothing to do with what looks like the wretched spawn of a Java programmer learning C++ from random Google searches :p – Quentin Mar 05 '18 at 15:47
  • 1
    @Quentin - This is exactly the sort of saying that got me accepting these comments. I confessed to getting a severe allergic reaction at the sight of such code, written by "recovering Java programmers". UKMonkey has been helping me build a tolerance since :) – StoryTeller - Unslander Monica Mar 05 '18 at 15:49
  • 1
    @StoryTeller #helping – UKMonkey Mar 05 '18 at 15:50
  • @StoryTeller: thanks for give to me the example I was looking for. But in the case of my code above, isn't easier just to resolve the call at compile time? Just to know. Sorry, I'm new of C++, I'm only learning it, not "random Google searches" really. – markzzz Mar 05 '18 at 15:54
  • 1
    @markzzz sorry, that wasn't about you! If you follow the duplicate link that I've provided, you will see that compilers can, in fact, devirtualize function calls when they have enough information :) – Quentin Mar 05 '18 at 15:58
  • 2
    @markzzz please ignore all these coments - we're just messing about. No one feels that you're a recovering Java addict doing random google searches. :) – UKMonkey Mar 05 '18 at 15:58
  • Ah ok, I see! Sorry :) I'll follow the link and try to learn more! Thank you. – markzzz Mar 05 '18 at 16:03
  • 1
    @markzzz - Oh boy, this is unfortunate. Let me apologize as well. None of the comments here were meant to criticize you. Like the good folks here said, it's just a silly running gag. We mentioned Java because it's garbage collector and similar syntax can cause developers to write unidiomatic code. I hope you weren't insulted. And btw, I was writing this comment as you were accepting the answer. – StoryTeller - Unslander Monica Mar 06 '18 at 09:39
  • Don't worry man! Thanks for the helps, as usual! – markzzz Mar 06 '18 at 09:48