Solved

Draw the memory layout of a C++ class with two virtual functions

Posted on 2006-07-11
3
644 Views
Last Modified: 2008-01-09
It's an interview question. Any help is appreciated.

Thanks!
0
Comment
Question by:gromul
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17085339
See http://en.wikipedia.org/wiki/Virtual_method_table

Assuming

class foo {

public:

virtual void f1();
virtual void f2();

};

foo:
  +0: pointer to virtual method table of foo

Basically, it is only the pointer to the VTABLE if the class has no other (data) members - 4 bytes, that's it.

If you have

class foo {

public:

virtual void f1();
virtual void f2();

int n;
};

that'd be

  +0: pointer to virtual method table of foo
  +4: value of 'n'
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 17085513
This is only a possible implementation, though it's extremely common, so I'd avoid saying this is THE layout.
It's conceivable that on some weird architecture they could do something else.

Good luck
0
 
LVL 4

Expert Comment

by:havman56
ID: 17097381
JKR  thanks for the link fom wikipedia

i learnt lot abt vtable  !!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now