For one, more abstraction or encapsulation.
Originally Posted by C_ntua
Secondly, very likely, it's much faster.
And thirdly, polymorphism was invented to be able to manipulate objects without knowing the exact type (even better with static polymorphism). In other words, polymorphism exists so that you shouldn't have to do downcasts!
Space is allocated for all member variables. "this" is not a member variable and thus no space is allocated for it. "this" is just a keyword that equals the address in memory where the instance of the class is stored. Basically it's just &myobj, where myobj is the instance of your class on the stack.
1) Space is allocated for all its variables and pointers, including the this pointer.
No, functions are global. They do not belong to a specific instance. They are functions with an extra parameter for "this".
2) Space is allocated to store the address of the function in the class and its manual and default constructors/deconstructors.
Constructors are the same - they, too, are functions.
Well, I suppose in a way.
3) So, if the function is virtual, then the address of the derived class's function is stored in the place of the base class virtual function
A usual approach to this (how virtual functions work are implementation defined!) is a v-table. Basically, the compiler stores a pointer to the correct function to be called for each instance of the class. When you create a Derived object, it stores the address of the Derived's function. The same applies if it's Base. Then Base's function is stored in the v-table.
However, this isn't guaranteed since each compiler vendor can do as they want, just as long as it works.
Yes, with the corrections. If object B is inside object A, then object B will be constructed while A is still constructing. When B finished constructing, only then will A finish constructing.
4) If an object is declared inside an object, then space is allocated with the above steps
Yes, the constructor's job is to make sure that the object is ready-to-use after you finish creating it.
5) In run time, the constructor assigns values and anything else it wants
Yes, this applies to any objects placed on the stack.
6) Space is freed when the object goes out of scope. So I suppose the compiler finds where the scope ends and calls the deconstructor function there.