Casting with multiple inheritance in C++ -
The following code is not compiled, and GCC -STD = C ++11
says that it Is a temporary static cast:
class A {public: virtual ~ A () {}}; Class B {Public: Virtual ~ B () {}}; Class Eddie: Public A {Public: Virtual ~ ed} () {}}; Class AB: Public B, Public A {}; Class ADB: Public B, Public Eddy {}; Int main () {ADB ADB; ADB * PTR = ADB; AB * Cast = Static_cast & lt; AB * & gt; (PTR); Return 0; }
I want to insert a class type from ADB
to AB
It should be safe to do this after , Who himself has himself a member of
A
followed by edi
. Thus, it appears that the compiler is forced to interpret ptr
as AB
:
class A {public: Virtual ~ A () {}}; Class B {Public: Virtual ~ B () {}}; Class Eddie: Public A {Public: Virtual ~ ed} () {}}; Class AB: Public B, Public A {}; Class ADB: Public B, Public Eddy {}; Int main () {ADB ADB; ADB * PTR = ADB; AB * cast = reintrip_en & lt; AB * & gt; (PTR); Return 0; }
This does not compile either, resulting in the Linker complaining about the undefined reference to Weible and the destroying the operator (zero *)
. .
At some point when explicitly, when the "podcast" is not "syncing", then in that case, I will not be able to use the AD
of ADB
Need to insert the individual base class of A
of AB
. I'm not sure about this, just guess How can I do this or its equivalent?
You can only successfully insert between the sections which the legacy structure of the object, therefore, , B or AD Your compatibility of the same memory layout does not apply. While understanding of the underlying implementation can provide helpful information specifically in some "how" and "why" (especially when it comes to performance), you should never consider the underlying implementation (or rationalize) To understand the rules) Language features It helps you to express higher level intentions.
More importantly, depending on the exploitation of the underlying implementation, you should never condemn language constraints. Although there may be occasionally a legitimate matter to do this, but in most cases, non-portable code or code will result in something that does not avoid the compiler's update - has failed to compile or break mysteries in production.
However, your assumption about memory layout may be wrong; In order to define virtual destructors, compiler is compelled to inject Vettel pointer in the memory layout of A, B, and AD, so the actual memory layout of ADB can not be that you think it is.
Comments
Post a Comment