Dynamic Binding Implementation Flashcards

1
Q
What are the steps of virtual function invocation in c++?
in the following example:
void foo(A* x) {
  x->f2();
}
A
  1. Read vptr at the head of the object,

    *reinterpret_cast(x)
  2. Read entry of f2
    Index known at compile time!
  3. Call address read on step 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens to the vpointer and vtable in a down-cast?

A

Nothing. the vptr and vtable don’t change, the only difference is that now the compiler “knows” about additional vtable entries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Does each (Polymorphic) object has its own vtable?

A

No. objects of the same type share a vtable, but every object has a vptr.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where is the vptr located in Borland style?

A

Virtual pointer is always located at the beginning of the object.
Easy access to vptr – always at the same offset (0).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where is the vptr located in GNU style?

A

VPTR is located at the beginning of the first sub-object that has virtual functions
Must add sizeof(Base) to reach vptr – on every virtual function call.
calculation is done at compile-time but the addition at run-time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When borland style is more efficiant? and when gnu?

A

borland - virtual function invocations

gnu - casting (no need to adjust pointer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is binding in constructors handled in c++?

constructor of base calls a virtual function overriden in derived

A

The binding of function calls within constructors

must be as if it is static.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the steps in constructing B, assuming B inherits from A?

A
  1. vptr points to A’s vtable
  2. construct A
  3. vptr points to B’s vtable
  4. construct B
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does an abstract class has a vtable?

A

yes. because when an inheriting class is constructed, the constructor of the abstract class is called and it’s vtable is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is binding within constructors works in Java?

A

fully dynamic.

In java there is an initialization phase before constructing, so all fields have default values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How dynamic languages implement dynamic binding?

A

Using a dispatch table, aka Method dictionary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is “as-if static binding” implemented in the constructors code?

A

בכניסה לבנאי הקומפיילר שותל קוד שמחליף את ה- vptr להצביע ל- vtbl של הטיפוס הסטטי. הקישור של כל
המתודות הוירטואליות ממשיך לעבור דרך ה- vptr ל- vtbl באינדקס המתאים, אך כיוון שה- vptr מצביע ל-
vtbl של המחלקה הסטטית - אנו מקבלים התנהגות שהיא “כאילו”-סטטית. ביציאה מהבנאי הקומפיילר שותל
קוד שמחליף בחזרה את ה- vptr להצביע למקום אליו הצביע קודם-לכן (ה- vtbl של הטיפוס הדינמי).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What problem can be in binding inside constructors in java?

A

reference types are initialized to null (default value), therefore if we try to dereference we will get a run-time exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly