Article directory
- 1. Overloading the equal sign = operator
-
- 1. Equal sign = operator and copy constructor
- 2. Overloading the equal sign = operator – the right operand is a String object
- 3. Overloaded operator functions corresponding to different right operands
- 2. Overloading the subscript [] operator
- 3. Complete code example
-
- 1. String.h class header file
- 2. String.cpp class implementation
- 3. Test.cpp test class
- 4. Execution results
1. Overloading the equal sign = operator
1. Equal sign = operator and copy constructor
The function of the equal sign = operator is to use an existing object to assign a value to another existing object;
- Note that it is different from the copy constructor. The copy constructor uses an existing object to initialize a new object;
- In the following code, the equal sign operator and copy constructor are called respectively;
String s1, s2; s1 = s2; // This is performed using the equal sign operator. String s3 = s2; // This is using the copy constructor
2. Overloading the equal sign = operator – the right operand is a String object
Use member functions to implement overloaded equal sign = operator:
- First, write the function name, The function name rule is “operate” followed by the operator to be overloaded,
-
2
2
Perform equal sign operation between two objects
Student s1, s2
, and use one existing object to assign a value to another existing object; - Usage is
s1 = s2
; - The function name is
operator=
;
-
operator=
- Then, Write the function parameters according to the operands, Parameters are generally references to objects;
- The equal sign operator is used as
s1 = s2
; - Left operand: The left operand is s1, It is called through this pointer, no declaration is required in parameters;
- Right operand – case ①: The right operand is also a String object; The operand needs to be declared in Among the parameters, please note that ordinary data types are declared directly, and object data types need to be declared as reference types;
- The above two are object types. Objects generally pass in pointers or references. Since they are basic data types, the basic data type is passed here; if it is an object type, then Pass in reference;
- Right operand – case ②: Another case is the usage of
s1 = "Tom"
, so that the right operand is of type char* string ;
- The equal sign operator is used as
operator=(const String & amp; s) // Pass in the String object and use const to modify the parameters to prevent the passed in object from being modified. operator=(const char* p) // Pass in string value
- Then, Perfect the return value according to the business, The return value can be a reference/pointer/element;
- The return value here returns String & amp; object reference, which can be used for other chain calls;
- If a String object is returned, it is a one-time anonymous object;
String & amp; operator=(const String & amp; s) // Pass in the String object and use const to modify the parameters to prevent the passed in object from being modified. String & amp; operator=(const char* p) // Pass in string value
- Finally, implement the function body, write specific operator operation business logic;
- First release the memory allocated by this object;
- Then perform the assignment operation;
3. Overloaded operator functions corresponding to different right operands
Overloaded operator functions corresponding to different right operands:
- When the right operand is a String object:
// Overload the equal sign = operator, when the right operand is a String object String & amp; String::operator=(const String & amp; s) {<!-- --> // First process the memory allocated by this object if (this->m_p != NULL) {<!-- --> //Memory previously allocated using new // To release memory, you need to use delete //Memory allocated using malloc needs to be released using free delete[] this->m_p; // Set the pointer to null to avoid wild pointers. this->m_p = NULL; //Set the string length to 0 this->m_len = 0; } //Copy string length // Note: The size of the memory space pointed to by the string pointer needs to be + 1, and the content is '\0' this->m_len = s.m_len; // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the string to the memory pointed to by m_p strcpy(this->m_p, s.m_p); cout << "Call the overloaded equal sign = operator function String & amp; String::operator=(const String & amp; s)" << endl; return *this; }
- When the right operand is a string pointer:
// Overload the equal sign = operator, when the right operand is a string constant value String & String::operator=(const char* p) {<!-- --> // First process the memory allocated by this object if (this->m_p != NULL) {<!-- --> //Memory previously allocated using new // To release memory, you need to use delete //Memory allocated using malloc needs to be released using free delete[] this->m_p; // Set the pointer to null to avoid wild pointers. this->m_p = NULL; //Set the string length to 0 this->m_len = 0; } //Copy string length // Note: The size of the memory space pointed to by the string pointer needs to be + 1, and the content is '\0' this->m_len = strlen(p); // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the string to the memory pointed to by m_p strcpy(this->m_p, p); cout << "Call the overloaded equal sign = operator function String & amp; String::operator=(const char* p)" << endl; return *this; }
2. Overloading the subscript [] operator
Use member functions to implement overloaded subscript [] operator:
- First, write the function name, The function name rule is “operate” followed by the operator to be overloaded,
- Subscript [] operator, the usage is
s[10]
; - Overloaded subscript [] operator function name is
operator[]
;
- Subscript [] operator, the usage is
operator[]
- Then, Write the function parameters according to the operands, Parameters are generally references to objects;
- Subscript operator is used as
s[10]
; - Left operand: The left operand is the s object, here called through this pointer, no need Declared in parameters;
- Right operand: The right operand is an int type index value;
- Subscript operator is used as
operator[](int i)
- Then, Perfect the return value according to the business, The return value can be a reference/pointer/element;
- The return value here is of char type, and returns the char type character at the specified index of the specific string;
- The char character is the value in an address in memory, and the reference type is returned here;
char & amp; operator[](int i)
- Finally, implement the function body and write specific operator operation business logic;
// Overload the array subscript [] operator char & String::operator[](int i) {<!-- --> // Directly return the corresponding i index character return this->m_p[i]; }
3. Complete code example
1. String.h class header file
#pragma once #include "iostream" using namespace std; class String {<!-- --> public: //Default parameterless constructor String(); // Constructor with parameters, receives a char* type string pointer String(const char* p); //Copy constructor, use String object to initialize object value String(const String & s); // destructor ~String(); public: // Overload the equal sign = operator, when the right operand is a String object String & amp; operator=(const String & amp; s); // Overload the equal sign = operator, when the right operand is a string constant value String & amp; operator=(const char* p); // Overload the array subscript [] operator char & amp; operator[](int i); private: // String length, excluding '\0' //Memory space size = string length + 1 int m_len; //String pointer, pointing to the string in the heap memory char* m_p; };
2. String.cpp class implementation
//Use strcpy function to report an error // error C4996: 'strcpy': This function or variable may be unsafe. // Consider using strcpy_s instead. // To disable deprecation, use _CRT_SECURE_NO_WARNINGS. // See online help for details. #define _CRT_SECURE_NO_WARNINGS #include "String.h" //Default parameterless constructor String::String() { //Construct an empty string by default, the length of the string is 0 // However, the size of the memory space pointed to by the string pointer is 1 and the content is '\0' m_len = 0; // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc m_p = new char[m_len + 1]; //Copy the empty string to the memory pointed to by m_p strcpy(m_p, ""); cout << "Call the parameterless constructor" << endl; } // Constructor with parameters, receives a char* type string pointer String::String(const char* p) { if (p == NULL) { //Construct an empty string by default, the length of the string is 0 // However, the size of the memory space pointed to by the string pointer is 1 and the content is '\0' this->m_len = 0; // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the empty string to the memory pointed to by m_p strcpy(m_p, ""); } else { // Get the length of the incoming string // However, the size of the memory space pointed to by the string pointer needs to be + 1, and the content is '\0' this->m_len = strlen(p); // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the string to the memory pointed to by m_p strcpy(m_p, p); } cout << "Call the parameterized constructor" << endl; }; //Copy constructor, use String object to initialize object value String::String(const String & s) { //Copy string length // Note: The size of the memory space pointed to by the string pointer needs to be + 1, and the content is '\0' this->m_len = s.m_len; // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the string to the memory pointed to by m_p strcpy(this->m_p, s.m_p); cout << "Call copy constructor" << endl; } // destructor String::~String() { if (this->m_p != NULL) { //Memory previously allocated using new // To release memory, you need to use delete //Memory allocated using malloc needs to be released using free delete[] this->m_p; // Set the pointer to null to avoid wild pointers. this->m_p = NULL; //Set the string length to 0 this->m_len = 0; } } // Overload the equal sign = operator, when the right operand is a String object String & amp; String::operator=(const String & amp; s) {<!-- --> // First process the memory allocated by this object if (this->m_p != NULL) {<!-- --> //Memory previously allocated using new // To release memory, you need to use delete //Memory allocated using malloc needs to be released using free delete[] this->m_p; // Set the pointer to null to avoid wild pointers. this->m_p = NULL; //Set the string length to 0 this->m_len = 0; } //Copy string length // Note: The size of the memory space pointed to by the string pointer needs to be + 1, and the content is '\0' this->m_len = s.m_len; // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the string to the memory pointed to by m_p strcpy(this->m_p, s.m_p); cout << "Call the overloaded equal sign = operator function String & amp; String::operator=(const String & amp; s)" << endl; return *this; } // Overload the equal sign = operator, when the right operand is a string constant value String & String::operator=(const char* p) {<!-- --> // First process the memory allocated by this object if (this->m_p != NULL) {<!-- --> //Memory previously allocated using new // To release memory, you need to use delete //Memory allocated using malloc needs to be released using free delete[] this->m_p; // Set the pointer to null to avoid wild pointers. this->m_p = NULL; //Set the string length to 0 this->m_len = 0; } //Copy string length // Note: The size of the memory space pointed to by the string pointer needs to be + 1, and the content is '\0' this->m_len = strlen(p); // Use the new keyword to allocate memory for the char* m_p; pointer // For basic data types new is equivalent to malloc this->m_p = new char[this->m_len + 1]; //Copy the string to the memory pointed to by m_p strcpy(this->m_p, p); cout << "Call the overloaded equal sign = operator function String & amp; String::operator=(const char* p)" << endl; return *this; } // Overload the array subscript [] operator char & String::operator[](int i) { cout << "Call the overloaded subscript [] operator function char & String::operator[](int i)" << endl; // Directly return the corresponding i index character return this->m_p[i]; }
3. Test.cpp test class
#include "iostream" using namespace std; //Import custom String class #include "String.h" int main() {<!-- --> //Call the parameterless constructor String s1; // Call the parameterized constructor String s2("Tom"); // Call the copy constructor String s3 = s2; //Call the overloaded equal sign operator function, the right operand is a String object s1 = s2; //Call the overloaded equal sign operator function, the right operand is a string constant value, char* pointer type s3 = "Jerry"; // Call the overloaded subscript operator function char c = s3[3]; //The console pauses, press any key to continue execution backwards system("pause"); return 0; }
4. Execution results
Execution results:
Call the parameterless constructor Calling a parameterized constructor Call copy constructor Call the overloaded equal sign = operator function String & amp; String::operator=(const String & amp; s) Call the overloaded equal sign = operator function String & amp; String::operator=(const char* p) Call overloaded subscript [] operator function char & String::operator[](int i)