C++ Learning Road (10): Execution Efficiency Introduced by Virtual Inheritance

I don't know what to name this article, so let's call it this way, just look at the scene and you'll understand. Excerpted from "Exploring the C++ Object Model in Depth"

Point3d origin, *pt = &origin;

(1)origin.x = 0;

(2)pt->x = 0

The execution results of the above two codes are the same, but is there any difference in execution efficiency?

(1) If Point3d is an ordinary structure, ordinary class, ordinary single inheritance or multiple inheritance, the execution efficiency of member x is exactly the same, because the position offset of x in the class is fixed at compile time (not introduced). virtual function). (2) If Point3d is virtually inherited from a base class, then pt cannot be determined at compile time which class type it points to, that is, the offset position of x cannot be determined at compile time. So this access operation must be delayed to the executor, which can be solved by an additional indirect boot, and the execution efficiency is slower than (1)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304278&siteId=291194637
Recommended