Data type conversions are often made in C + +, such as integer data that can be assigned to floating-point variables, and integer data is converted to floating-point types before being assigned, and in turn, floating-point data can be assigned to integer variables.
The premise of data type conversions is that the compiler knows how to trade-offs with the data. For example:
int 10.9 ;p rintf ("%d\n", a);
The output is 10, and the compiler discards the fractional part directly (not rounding). Again such as:
float Ten ;p rintf ("%f\n", b);
The output is 10.000000, and the compiler automatically adds the fractional part.
Class is also a data type, and data type conversions can occur. However, this conversion is only meaningful between the base class and the derived class.
Because a derived class contains members that inherit from a base class, you can assign an object of a derived class to a base class object, as follows:
class a{public: int m;}; class Public a{public: int= 2;
A is the base class for B, and A and B are their objects, so you can assign B to a and then access the member variable m through a.
In fact, the assignment between objects is the assignment of member variables, and there is no assignment problem for member functions. When you assign a value, you discard the members of your derived class, which is "overqualified", as shown in:
It can be found that, even if the derived class object is assigned to a base class object, the base class object does not contain members of the derived class, so the members of the derived class are still accessed through the base class object. For the above example, the A.M. is correct, but A.N is wrong, because a does not contain member N.
This conversion relationship is irreversible and can only be assigned to a base class object with a derived class object, but not to a derived class object with a base class object assigned a value. The reason is that the base class does not contain members of the derived class and cannot assign a value to the member variable of the derived class. Similarly, a value cannot be assigned between different derived class objects of the same base class.
To understand this problem, we have to start with the nature of the assignment. The assignment is actually to fill in the data to the memory, when the data is very good processing, discard it; In the above example, when A=b, member n is redundant and is discarded directly, so no assignment error occurs. But when the data is small, the problem is tricky, the compiler does not know how to populate the rest of the memory, in the above example, b= A, the compiler does not know how to assign a value to the variable n, so an error occurs.
Take a look at the following complete example:
#include <iostream>using namespacestd;classa{ Public: intN; A (intN): N (n) {}voidDisplay () {cout<<"Class a:n="<<n<<Endl;}};classB: Publica{ Public: B (intN): A (n) {}voidDisplay () {cout<<"Class b:n="<<n<<Endl;}};classC: Publica{ Public: C (intN): A (n) {}voidDisplay () {cout<<"Class c:n="<<n<<Endl;}};intMain () {A A (1); b b (2); C C (3); A.display (); A=b; B.N= -; A.display (); A=C; A.display (); return 0;}
In this example, the B object is assigned to the A object, equivalent to A.N = B.N, the value of A.N is 2 after the assignment is completed, and then the A.display () function is called, and the output is "Class a:n=2". The same is true for assigning a C object to a object.
This example is a good illustration: the assignment between the base class object and the derived class object is only the assignment of the corresponding member variable, does not affect the member function, and does not affect the this pointer.
Summary: A member of a derived class can be accessed through a derived class object, but in any case, the derived class member cannot be accessed through the base class object.
Pointer to Object
Make the following changes to the code in the main function of the previous example:
New A (1);p,new B (2);p;p New C (3);p->display ();
Output Result:
Class A:n=1
Class a:n=100
Class a:n=3
This example defines a pointer so that it points to a different object. Unlike the previous example, the assignment of an object does not occur in this example, and it simply changes the pointer's direction.
The 2nd line of the output is different from the previous example, which is why?
In line 3rd of the code, p points to the Class B object, the implicit pointer this also changes, pointing to the Class B object, so p->n and This->n access is a Class B object member variable, the output is obviously "n=100".
The attentive reader may have discovered that although this points to a class B object, P->display () still calls the member function of Class A. This is because member variables and member functions are not in the same memory region, and the system accesses member variables through the this pointer, but does not access member functions through it.
The system accesses member functions through the type of the object. For example, if the type of P is a, the member function of Class A is accessed regardless of which object P points to.
C + + Learning 21 Assignment of base classes and derived classes