C + + learning starts from scratch (ii)

Source: Internet
Author: User
Tags inheritance

Multiple inheritance

Here's an interesting question, as follows:

struct A {Long A, B, C; Char D;}; struct B:public A {long E, f;};

What is the offset of the above b::e and b::f mappings? Different compilers have different mapping results, and C + + does not impose rules for derived implementations. Most compilers allow the B::e map to have an offset value of 16 (that is, a length, the length of the custom type can refer to C + + zero-based (ix)), B::f mapping 20. This is equivalent to leaving space to arrange the member variables of the parent class, and then arrange your own member variables. But there is this semantics-tomatoes are vegetables and fruit, whales are marine life and breast-milk animals. That is, an instance is both this type and that type, for which C + + provides multiple derivation or multiple inheritance, with the "," interval of each parent class, as follows:

struct A {long a_a, a_b, C; void ABC (); struct B {long C, b_b, b_a; void ABC

(); };

struct Ab:public A, public B {long AB, C; void ABCD ();

void A::abc () {a_a = A_b = n = 20;}

void B::abc () {b_a = B_b =; c = 10;}

void Ab::abcd () {a_a = B_a = 1; A_b = B_b = 2; c = a::c = B::c = 3; }

void Main () {AB ab; ab. A_a = 3; Ab. B_b = 4; Ab. ABC (); }

The above structure AB derives from structure A and structure B, that is, we can say that AB is both an instance of a and an instance of B, and an instance of AB. How many mapping elements are generated when you derive AB? According to the previous article, in addition to the AB::AB and ab::c defined in the type definition character "{}" of AB (the type is long ab::), also generate inheritance

To the mapping element, the modification of the name of each mapped element is replaced by AB::, the type is invariant, and the value of the map is unchanged. So for two parent classes, 8 mapping elements are generated (each class has 4 mapping elements), such as one whose name is Ab::a_b, the type is long a::, the value of the map is 4; there is also a ab::b_b, the type is long B::, the value of the map is still 4. Note that the names of the A::ABC and B::ABC are the same, so the names of the two mapped elements are ab::abc, but the type is void (A::) () one is void (B::) () and the mappings are A::ABC and B::ABC respectively. Similarly, there are three mapping elements whose names are ab::c, and the types are long A::, Long B:: And Long AB::, the mapping offsets are 8, 0, and 28, respectively. As I said before, the member variables of the parent class are arranged and the member variables of the subclass are sorted, so the type long AB:: The value of the AB::C mapping is the sum of the lengths of the two parent classes plus the Ab::ab offset. Notice the problem, with two pairs of the 8 mapped elements that inherit the generated above, there are no problems, however, because they are of different types, and the final compiler modifies their names to form symbols based on their respective types, so that the connection will not have a redefinition problem, but other problems arise. Ab. ABC (); it must be AB. AB::ABC (); abbreviated because AB is AB type, but now because there are two ab::abc, it is written AB directly above. ABC will error, because can not know is to which ab::abc, then what to do?

Recall the public, protected, private inheritance mentioned in this article, which said that public means that an instance of a subclass can be treated as an instance of a parent class. That is, where all the instances of the parent class need to be used, if it is a subclass instance and the relationship between them is a public inheritance, the compiler will make an implicit type conversion to convert the subclass instance to the parent class instance. So the ab above. A_a = 3; is actually AB. Ab::a_a = 3, and the type of ab::a_a is long A::, the member operator requires the same type as both sides, the left type is AB, and AB is a subclass, so the compiler will automatically implicitly type convert, and the instance of AB into an instance of a, and then the member operator is evaluated.

Note that the offset values for Ab::a_b and Ab::b_b are 4, AB. A_b = 3; it is not equivalent to AB. B_b = 3;? Even according to the above, because the types of ab::a_b and Ab::b_b are long a:: And long B:: And most of the former converts to a instance of the latter to B, the offset of the ab::a_b and ab::b_b mappings remains the same. So the number on the left of the member operator is changed. For structure AB, suppose you first arrange the member variables of the parent class A and then arrange the member variables for the parent class B. The offset of the ab::b_b mapping should be 16 (the length of struct a plus the offset introduced by B::C), but it actually maps to 4, so add the number of the address type to the left of the member operator with 12 (the length of the structure a )。 For Ab::a_b, because the member variables of struct A are first arranged, they are only offset by 0. Suppose the above AB corresponds to the address 3000, for AB. B_b = number of address types of 4;,ab type 3000 in "." , the number 3012 (because of the offset 12) of the type of address Type B is converted to, and then the "." The number 4 of the offset type on the right plus 3012, finally returns the number 3016 of the address type of type long, and then continues to compute "=". You can also know AB. A_a = 3, the member operator in the end returns the number 3000 of the type of address of type a long, and AB. A_b will return 3004,ab.ab will return 3024.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.