1

I want to understand the reason behind the output of the following C++ programs involving virtual functions. Please also explain how the function pointer table and virtual pointer table containing links to function pointer tables will be generated in the following 2 cases and how the call is resolved at runtime.

/******* PROGRAM 1 *******/
#include <iostream>
using namespace std;

class Car {
    public:
    virtual void foo() {
        cout<<"Car"<<endl;
    }
};

class Bmw: public Car {
    public:
    void foo1() {
        cout<<"Bmw"<<endl;
    }
};

int main() {
        Car *c = new Bmw(); 

        c->foo();           // gives output Car even though foo() 
                            //function does not exist in BMS class. 

        return 0;
}

/******* PROGRAM 2 *******/
#include<iostream>
using namespace std;

class Car {
    public:
    virtual void foo() {
        cout<<"Car"<<endl;
    }
};

class Bmw: public Car {
    public:
    void foo() {
        cout<<"Bmw"<<endl;
    }
};

class Bmw7: public Bmw {
    public:
    void foo1(){
        cout<<"Bmw7"<<endl;
    }
};

int main() {
    Car *c = new Bmw7();

    c->foo();       //gives output Bmw. Why output is not Car ??
    return 0;
}
shubhamcr7
  • 37
  • 1
  • 5
  • 4
    `foo1` isn't `foo`. You may find [`override`](https://en.cppreference.com/w/cpp/language/override) helpful – user4581301 Apr 10 '19 at 04:20
  • 1
    You may also wish to consult [this list](https://stackoverflow.com/q/388242). Nothing like a good vetted book to help one wrap their head around a complex language like C++. – StoryTeller - Unslander Monica Apr 10 '19 at 04:27
  • @user4581301 Why don't you consider that an answer? – Yunnosch Apr 10 '19 at 06:22
  • @Yunnosch Looked too much like a typo to me. If it wasn't, there's not much value to future programmers in "The identifiers must match.". If you don't know that by the timer you've reached Stack Overflow, your reference material is dodgy at best. – user4581301 Apr 10 '19 at 15:06
  • @user4581301 There is no typo in the above code snippet given in the question. I was asked this question in Arcesium's interview and was not able to do it. I got rejected and the first thing I did after that was to post this question here. 2 years have passed and I am currently working at Arcesium's parent company D.E. Shaw, as a C++ developer(SDE-II). The accepted answer and all comments make perfect sense to me now. – shubhamcr7 May 21 '21 at 15:43
  • Agreed I don't think I read the question closely enough to understand what you were really asking. – user4581301 May 21 '21 at 16:34

1 Answers1

0

Here is a pretty good explanation of virtual functions and vtables.

Every class that uses virtual functions (or is derived from a class that uses virtual functions) is given its own virtual table

Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

This pretty much answers your questions. In the first example the most-derived function accessible by c is Car's foo. And in the second one it is Bmw's foo. Here even though you didn't write virtual in front of foo(which is not such a good coding style) in Bmw, its virtualness is inherited from Car.

EDIT: As it was correctly stated in the comment the vtables are not part of the standard. See this reference for a more formal explanation.

For every virtual function, there is the final overrider, which is executed when a virtual function call is made. A virtual member function vf of a base class Base is the final overrider unless the derived class declares or inherits (through multiple inheritance) another function that overrides vf.

Community
  • 1
  • 1
Nellie Danielyan
  • 1,001
  • 7
  • 19
  • 1
    Note: C++ does not require the use of VTables. VTables are by far the most common implementation because it does the job in a direct and relatively easy to implement manner, so it's what you're most likely to see. Meanwhile in secret labs around the world top scientists are working on alternatives, always seeking for a better solution. – user4581301 Apr 10 '19 at 16:41
  • Thank you, yes, just provided this answer as the question was specifically about vtable. Edited. – Nellie Danielyan Apr 10 '19 at 19:20