Inside the Overriding Mechanism

From Andrey

(Difference between revisions)
Revision as of 07:00, 3 February 2006
Andrey (Talk | contribs)

← Previous diff
Revision as of 07:03, 3 February 2006
Andrey (Talk | contribs)

Next diff →
Line 10: Line 10:
Step 5 needs further explanation. When entering the override, this points to the member for which the function has is been called. But the code of the override resides in the owner class, and deals with the owner class. It requires this pointer to point to the owner object. Therefore, this is modified - by subtracting the offset between the owner and the member object. The place of the member inside the owner class is known in the compile time, so the modification of this is merely the subtracting of a constituent. Step 5 needs further explanation. When entering the override, this points to the member for which the function has is been called. But the code of the override resides in the owner class, and deals with the owner class. It requires this pointer to point to the owner object. Therefore, this is modified - by subtracting the offset between the owner and the member object. The place of the member inside the owner class is known in the compile time, so the modification of this is merely the subtracting of a constituent.
-[[Image:Overriding.gif|center|Overriding of a virtual function in the owner class]]+[[Image:Overriding.gif|center|thumb|320px|Overriding of a virtual function in the owner class]]

Revision as of 07:03, 3 February 2006

Among possible implementations of overriding in owner class the most natural is to use the same mechanism of dynamic binding through the virtual method tables as for the usual overriding. The implementation described below is simple, straightforward, does not require special data structures, does not interfere with other language features, and does not require additional memory. The efficiency of a call to an overridden member’s method is the same as of a call to a virtual function of an object, when not optimized by knowing the exact type.

The process of overriding in owner class consists of the following steps: 1. A new virtual method table is created for the member. 2. The new table is initialized with the data from the original table. 3. In the new table, the address of the original function(s) is changed to the address of the function override(s). 4. The pointer to the virtual method table in the member object is modified to point to the new table. 5. When entering the body of the function override, this pointer value is modified to point to the owner object.

Step 5 needs further explanation. When entering the override, this points to the member for which the function has is been called. But the code of the override resides in the owner class, and deals with the owner class. It requires this pointer to point to the owner object. Therefore, this is modified - by subtracting the offset between the owner and the member object. The place of the member inside the owner class is known in the compile time, so the modification of this is merely the subtracting of a constituent.

Overriding of a virtual function in the owner class
Enlarge
Overriding of a virtual function in the owner class