C
Christopher
Is the following correct?
----
Question:
On a 32 bit machine, if you have a class A with one int (or DWORD) as
member data, along with a virtual destructor and one other virtual
method, what will the sizeof(A) return?
Answer:
The sizeof function returns the size of a type in bytes.
With no virtual methods, sizeof(type) returns the sum of the class'
data members. However, when you make one or more methods virtual, the
class now carries with it a pointer to the class' vtable.
The answer is therefore, 4 bytes for the int + 4 bytes for the pointer
to the vtable = 8 bytes.
----
What exactly does the vtable look like and how are methods "looked
up"?
The class just contains a pointer to the vtable? I thought each method
had a pointer into the vtable...
I wrote my test program and sure enough I got 8. I flubbed this on an
interview recently, so I want to make sure I've got it down for next
time.
----
Question:
On a 32 bit machine, if you have a class A with one int (or DWORD) as
member data, along with a virtual destructor and one other virtual
method, what will the sizeof(A) return?
Answer:
The sizeof function returns the size of a type in bytes.
With no virtual methods, sizeof(type) returns the sum of the class'
data members. However, when you make one or more methods virtual, the
class now carries with it a pointer to the class' vtable.
The answer is therefore, 4 bytes for the int + 4 bytes for the pointer
to the vtable = 8 bytes.
----
What exactly does the vtable look like and how are methods "looked
up"?
The class just contains a pointer to the vtable? I thought each method
had a pointer into the vtable...
I wrote my test program and sure enough I got 8. I flubbed this on an
interview recently, so I want to make sure I've got it down for next
time.