In the last instance program code in the previous article, the parameter variables in the member functions I wrote are the same as the data member names. In order to avoid errors during compilation, I add "Class Name:" Before the data member to differentiate. In fact, there is another way to distinguish between them, that is, the self-reference pointer this in C ++. Today, let's talk about this and string classes in C ++. There may not be a lot of code, but the principle is highlighted;
1. This keyword is actually no stranger to me. It is often used in C. But today I want to explain why this and this play a role? We still need to start with classes and objects. We all know that to define an object, the system will allocate storage space to this object. If this class contains data members and member functions, we need to allocate storage space for data and Function Code separately. According to the normal idea, if the class defines two objects, allocate space for the data and functions of the two objects respectively. However, this is not the case. The C ++ compilation system only uses a space to store it in common function code segments. when calling the member functions of each object, and call this public function code. Therefore, the storage space of each object is only the storage space occupied by the data members of the object, excluding the space occupied by the member function code. Function Code is stored outside the object space. But the problem arises. Since the code of the member functions called by all objects is only one, how does the member function know which object is called currently?
This is a self-referenced pointer. Whenever an object is created, the system initializes this pointer to point to this object, that is, the value of this pointer is the starting address of the object currently calling the member function. Every time a member function is called, The system sends this pointer as an implicit parameter to the function. When different objects call the same member function, the C ++ compiler determines the data member of the object to be called Based on the object pointed to by the this pointer of the member function. The following is a simple example.
1 #include "stdafx.h"
2 #include <iostream>
3 #include <string>
4
5
6 class Num
7 {
8 private:
9 int a;
10 std::string objectName;
11 public:
12 Num(std::string objectName,int a);
13 void showNum();
14 };
15
16 Num::Num(std::string objectName,int a)
17 {
18 this->objectName=objectName;
19 this->a=a;
20 }
21
22 void Num::showNum()
23 {
24 std::cout<<this->objectName<<":this="<<this<<" a="<<this->a<<std::endl;
25 }
26
27
28 int main()
29 {
30 Num num1("num1",1);
31 Num num2("num2",2);
32 Num num3("num3",3);
33
34 num1.showNum();
35 num2.showNum();
36 num3.showNum();
37
38 return0;
39 }
Result:
2. In the previous code, I used the char * string pointer to define character variables, and the constructor in the kid class in the previous article, which used character arrays. Use the character replication function strcpy to solve the problem and solve it with the help of Boyou. Strcpy, strcat, and strlen are all string operations of the standard library functions in C. C ++ retains this format, but this method is not easy to use. In addition, the separation of data from the functions used to process data does not conform to the object-oriented idea. Therefore, the string class string is displayed in the standard library of C ++. When using the string type, you must add # include <string> to the header file. The use of string is also useful in the preceding example. For example, string STR ("Hello C ++") or string STR = "Hello C ++, you can also mix the string object with the '\ 0' terminator in the expression.
3. The following table describes the common operators in string: