Goldman Sachs Interview Question
Software AnalystsCountry: United States
Interview Type: In-Person
very first answer is correct ! Each class having virtual function in it, will have single virtual table. But each object of such class will have virtual pointer in it.
so base 50 objects will have 50 virtual pointers (one per each)
same for derive.
Just for example.
class base
{
public:
virtual fun1()
{
}
};
class derive : public base
{
public:
virtual fun2()
{
}
};
int main()
{
derive b1, b2;
printf("%d %d\n", sizeof(b1) , sizeof(b2));
derive d1, d2;
printf("%d %d\n", sizeof(d1) , sizeof(d2));
}
you will get answer :- 4,4,4,4,
here 4 is the virtual_pointers size.
Please correct me if I'm wrong ...
- Anonymous August 17, 2013There is a single static v-table per class and all objects of a polymorphic class share it.
However , the compiler adds a virtual pointer to each object of this polymorphic class .
and inside the constructor initializes it to point to the v-table .... So I believe Virtual pointers(v-ptrs) will be 50 per base class object and 50 per derived class object .....