How does the internal conversion between the derived and base class take place and how does the compiler know or does it save the size of the object?
For example, in the following:
class A { public: A():x(2){}; private: int x; }; class B : public A { public: B():A(),y(5){}; private: int y; }; class C : public B { public: C():B(),z(9){}; private: int z; }; int main() { C *CObj = new C; B *pB = static_cast<B*>(CObj); delete CObj; }
Edit: Perhaps this was:
B BObj = static_cast<B>(*CObj);
You do not have any "base to base" conversion in your code. What you have in your code is converting the pointer to the base one. (This conversion does not require any explicit translation, BTW)
B *pB = CObj; // no need for the cast
To perform pointer conversion, there is no need to know the size of the object. Thus, it is not clear where your link to "object size" comes from.
, . , . . , .
(, ) , . . , .. , , .
, , , , , . , .
: static_cast; "" "-" "-".
static_cast
. (.. , CObj pB , ). B C , . , ( new C).
CObj
pB
B
C
new C
- , vtables vptrs (http://en.wikipedia.org/wiki/Vtable).
. , 10.3
", (1.8) "
, , , , . , Derived * Base * , , .
, , , . VTable VPTR, . " ", . , "", "", "" . , . " ". , "", .
., , .( ).
[ ]
( C).
?
, , .( ), , ( ), ( , , ).
- C, .
, . , . C int [4]. , , .
Source: https://habr.com/ru/post/1764351/More articles:Detect deferred support script without testing - javascriptunit tests DAO - unit-testingCapturing icons with node.js - node.jsA service configured in spring / resources.groovy does not have a Hibernate session - grailsRetrieve random object from data store - pythonMoq Event Aggregator Is it Possible - moqhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1764353/openx-allowing-banner-links-to-use-the-host-of-the-page-the-banner-is-being-displayed-on&usg=ALkJrhhH1mek9ocgkPMko_ROSSe4JbZtngHow to delay function inside jQuery-ui slider event - javascriptSSO with OpenID - authenticationHow to match a vector to a map by clicking on it with duplicate key values? - hashmapAll Articles