In the C ++ environment, there are two concepts: upward type conversion and downward type conversion, which respectively describe the forced type conversion from subclass to base class and base class to subclass, therefore, you can feel interesting and attractive in the use process.
- Void swap (int & a, int & B)
- {
- //...
- }
- 2. write out the following structure or class size (output value of sizeof) and give a brief explanation.
- //:
- Typedef struct
- {
- Char c1;
- Char c2;
- Int n;
- } Stru;
- // B:
- Class ClassA
- {
- Public:
- Int m_data1;
- Int m_data2;
- Void func1 (){}
- Void func2 (){}
- Virtual void vfunc1 (){}
- Virtual void vfunc2 (){}
- };
- // C:
- Struct ClassB {
- Void f ();
- };
- B: f (){
- }
If you are interested, you can try to make your own answers without looking at the correct answers below ........
1. the test point of the original question is Y (): B ("B"), a ("a"), X ("base") {} constructor Y () the string after the colon (:). To be honest, I only remember it was related to initialization at the beginning. The correct name is actually "initialization parameter list ". its key role is to complete the initialization of some data before entering the constructor. In program1, it means that the initialization of object B and a is prior to that of the Y () constructor, B ("B"), a ("a") is equivalent to X B (X ("B"); X a (X ("")) that is, it is done by copying the constructor.
2. someone may ask how to use X ("base") in this way. In fact, it is very simple to note that Y is a subclass of X. The relationship between the subclass and the parent class constructor is unknown, that is, first call the parent class constructor. Here, X ("base") specifies that () the constructor of the parent class X called before the constructor is a C ++ environment rather than X (). Here there are some detours. You can try to replace X ("base") with X (), the specific meaning is clearly defined.
3. the last key point is the output sequence of the results of program1. I guess some people have two meanings: B, a, base, base, B, and a. First, the main function declares y, so the correct execution sequence should be the constructor of the parent class X of Y (note that the list of Y initialization parameters specifies which constructor should be called by the parent class X of Y) ------> constructor of the Private member variable of Y (note that the list of initialization parameters of Y specifies which constructor should be called by the private member variable) ------> Y constructor, for program1, you need to call X ("base") first ");
Second, the calling sequence of the constructors of all private member variables is consistent with that of the variable to be called, but is irrelevant to the order of the variable to be called. For program1, calling the Y constructor calls the order of private variables first B and then a, while declaring that the order of private variables is first a and then B. Therefore, the latter is the correct result. In summary, the call sequence of the constructor is X ("base"), X ("a"), X ("B"), and Y ()
4. to understand the above three points, let's take a look at program2, which is much simpler. program2 does not have any traps. The rules are constructed according to the parent class, private variables, and subclasses, the first XXX in the output result is the construction of the parent class X of Y, and the second and third C ++ environments are the construction of the and B variables (pay attention to the Order) in class Y, respectively, finally, the four outputs in the Y structure.
It seems convenient to add static values, but how do you know you should allocate 256 bytes? But if the heap is needed for Dynamic Allocation, who will release the heap? Therefore, I personally think the best way is to add a str parameter to this function, which is filled by the customer programmer. In this way, you can control errors. But if I really don't want to do this, I think I can only use smart pointers. There are too many smart pointers.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design