Bloomberg LP Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

- A vtable is created for each class.
- That means if B derives from A, there will be two vtables
- the vtable is populated during compilation
- All objects of B have a pointer (vptr) to the vtable of B
- All objects of A have a pointer to the vtable of A
- all functions marked "virtual" are looked in the vtable of the actual object created, if found they are invoked
- If not found lookup is performed in the vtable of the immediate base class and so on

- Samba December 21, 2005 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"If not found lookup is performed in the vtable of the immediate base class and so on"
That cant happen, the virtual members of the base if not overridden in the derived class, the address of the base member function is stored in the vtable of the derived class.

Eg:
if the base class virt2 function is not overridden then the derived class vtable will have
&Der::virt1, &Base::virt2

- San October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't know either but I would've guessed that a vtable is created for each class. Computer architecture is very predictable if you know what I mean :oP

- Jack January 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For each class which has at least 1 virtual table, the compiler build a virtual table with the addresses of those virtual functions.
In the constructor, each object gets a VPTR, which is a pointer to the VTable.
In inheritance, the base class's consrtuctor is called first. It creates the VPTR and initializes it to the Base VTABLE.
The derived class consrtuctor is called then, and updates the VPTR to point the Derived VTABLE.
The final state of the VPTR is determined by the consrtuctor who called last.

When a Base pointer points to a derived object and calls a virtual method, the compiler translate this call to something like:
pBase -> VPTR -> right place in the VTABLE -> right method.

- Miri January 08, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is more complete. thanks Miri.

Seshagiri

- Seshagiri January 08, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is a vtable for each class that has atleast one virtual function. The first member of each object of a class is the pointer to the v-table (This member is not visible to the programmer but is an internal representation). So when the base class pointer points to the derived class, it has the pointer to the v-table that has an entry for the method to call (which is either the method in the base class if the derived class does not implement it or the method in the derived class itself).

- Atul Gupta January 13, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For this question, I think they are asking for the Virtual Table Implementation. I have surfed the web but couldn't find much info on this.

Here is an example of what they were asking for.

class A {
virtual void foo() { }
void bar() {}
}

class B {
foo() {}
bar() {}
}

int main(void) {
A *aptr = new B();
aptr->foo(); //This would call B's foo()
aptr->bar(); //This would call A's bar()
return 1;
}

The question comes down to, how does the compiler model the Virtual Table such that it knows to call B's foo() and A's bar(). I have read quite a few C++ books and none really go into depth about this. Even if they did, I wouldn't remember but I know that "The Design and Evolution of C++" book written by Bjarne Stroustrup does mention this. It has some complicated rules dealing with Virtual Table Implementation. I need to go back and read this book.

- Khoa February 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Because when you say aptr->foo, the compiler translates it into

foo(aptr)

Another example

class X {}

int main()
{
X xx;
xx.foo(); //this is essesially foo(&xx);
return 0;
}

remember that every function gets passsed a "this" pointer, which is the address of the calling object. This is why we can't have static virtual functions, because they have no "this" pointer passed to them.

- Ozzy May 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://en.wikipedia.org/wiki/Virtual_table

- Krishna March 18, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How do you find questions like these? I mean, I have taken a course in C++ but I never learned about vtables and stuff? Is there any advanced programming book in C++ that someone can recommend to me?

Thanks!

- Robert March 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you guys are REALLY interested in learning C++ Vtables etc, you can read this book - Inside the C++ Object Model - by LipMan. He gives a deep insight of all the C++ details. - thanks - siva

- siva June 02, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think to get acquainted with this topic Bruce Eckel "Thinking in C++" Part-I should be used.

- S S Verma March 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bruce Eckel is the way to go

- Ska March 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A good explanation here
hxxp://www.learncpp.com/cpp-tutorial/125-the-virtual-table/

- Ronin February 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is How it would look
http://www.codesourcery.com/public/cxx-abi/cxx-vtable-ex.html

- Ano October 14, 2009 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More