C + + A common face test is to write a simple string class, this class should have the necessary constructors, copy constructors, assignment operators, destructors and other functions, the need to deal with memory problems.
Many ways to assign operators:
1. The general classical writing is (does not consider exception handling): Here is also to note: The assignment operator returns a reference to *this: See effective C + + in detail
string& string::operator= (const string& RHS) {
if (this = = &RHS) {
return *this;
}
Delete[]_data;
_data = new Char[rhs.size () + 1];
strcpy (_data, rhs._data);
}
There may be an exception in the process of making new, because there is not enough memory available.
2. Copy and Swap Technology
void Swap (String rhs) {
Std::swap (_data, rhs._data);
}
string& operator= (const string& RHS) {
The String temp (RHS); Using the copy constructor
Swap (RHS);
return *this;
}
3. Transfer value
string& operator= (String RHS) {//Here's the value, just the copy.
Swap (RHS);
return *this;
}
4.c++11 Right-value reference
string& operator= (string&& RHS) {
Swap (RHS);
return *this;
}
The rest of the feeling there is no difficulty, the complete code is as follows:
date:2016.11.6
Autore:yqtao
/*
Implementation of the String class
*/
#ifndef String_h
#define String_h
#include <iostream>
using namespace Std;
Class String {
Private
Char* _data;
Public
String (const char* str = nullptr);
String (const string& RHS): _data (New Char[rhs.size () + 1]) {
strcpy (_data, rhs._data);
}
string& operator= (const string& RHS);
~string () {
if (_data!= nullptr) {
Delete[]_data;
_data = nullptr;
}
}
void Swap (String rhs) {
Std::swap (_data, rhs._data);
}
size_t size () const {
return strlen (_data);
}
Private
Overloaded operators
char& operator[] (int index);
Friend ostream& operator<< (ostream &os, const String &RHS);
Friend istream& operator >> (IStream &is, String &RHS);
Friend String operator+ (const string& LHS, const string& RHS);
Friend String operator+= (string& lhs,const string& RHS); Note that this cannot be declared as a const
friend bool operator== (const string& LHS, const string& RHS);
friend bool operator!= (const string& LHS, const string& RHS);
friend bool Operator> (const string &LHS, const string &RHS);
friend bool Operator>= (const string &LHS, const string &RHS);
friend bool operator< (const string &LHS, const string &RHS);
friend bool Operator<= (const string &LHS, const string &RHS);
};
string::string (const char* str) {//constructor, default input null
if (str = = nullptr) {
_data = new Char[1];
*_data = ' n ';
}
else {
_data = new Char[strlen (str) + 1];
strcpy (_data, str);
}
}
//
string& string::operator= (const string& RHS) {
if (this = = &RHS) {
return *this;
}
Delete[]_data;
_data = new Char[rhs.size () + 1];
strcpy (_data, rhs._data);
}
/*
Other wording:
2. Copy and Swap Technology
string& operator= (const string& RHS) {
The String temp (RHS); Using the copy constructor
Swap (RHS);
return *this;
}
3. Pass Value method
string& operator= (String RHS) {
Swap (RHS);
return *this;
}
4. C++11 Right Value reference
string& operator= (string&& RHS) {
Swap (RHS);
return *this;
}
*/
char& string::operator[] (int index) {
return _data[index];
}
ostream& operator<< (ostream &os, const String &RHS) {
Os << rhs._data;
return OS;
}
/*
Note that there may be an error here, and if there is any reason for the great God, please advise
istream& operator >> (IStream &is, String &rhs) {
String tmp;
is >> tmp._data;
RHS = tmp;
return is;
}
*/
Overload +
String operator+ (const string& LHS, const string& RHS) {
String Res;
int len = lhs.size () + rhs.size ();
Res._data = new Char[len + 1];
strcpy (Res._data, lhs._data);
strcat (Res._data, rhs._data);
return res;
}
Overload = =
String operator+= (string& lhs, const string& RHS) {
LHS = LHS + RHS; can be completed with addition
return LHS;
}
Overload = =
BOOL operator== (const string& LHS, const string& RHS) {
if (strcmp (lhs._data, rhs._data) = = 0) return true;
return false;
}
BOOL Operator!= (const string& LHS, const string& RHS) {
if (strcmp (lhs._data, rhs._data) = = 0) return false;
return true;
}
Overloading > functions
BOOL Operator> (const string &LHS, const string &RHS) {
if (strcmp (Lhs._data, Rhs._data) > 0)
return true;
return false;
}
BOOL operator< (const string &LHS, const string &RHS) {
if (strcmp (Lhs._data, Rhs._data) < 0)
return true;
return false;
}
Overloaded <= function
BOOL Operator<= (const string &LHS, const string &RHS) {
if (strcmp (Lhs._data, Rhs._data) <= 0)
return true;
return false;
}
Overloaded >= function
BOOL Operator>= (const string &LHS, const string &RHS) {
if (strcmp (Lhs._data, Rhs._data) >= 0)
return true;
return false;
}
#endif//! String_h
Test:
#include "String.h"
int main () {
String s;
cout << s<<endl;
String S1 = "Test";
cout << s1 << Endl;
String S2 (S1);
cout << S2 << Endl;
s = s2;
cout << s << endl;
/*
String S3;
cout << "Please input a string" << Endl;
CIN >> S3;
cout << S3 << Endl;
*/
//
String s4 = "Hello";
String S5 = "World";
String s6 = s4 + S5;
cout << S6 << Endl;
S6 + = S4;
cout << S6 << Endl;
cout << (s6 = = S4) << Endl;
String S7 (S6);
cout << (s6 = = s7) << Endl;
//
cout << (S4 > S5) << Endl;
cout << (S6 > S5) << Endl; S6=helloworld,s5=world, so this sentence should be false
cout << (S6 > S4) << Endl;
return 0;
}
Problem encountered: Memory error occurred while overloading >>. If you know what the reason is, please let me know,thank you
istream& operator >> (IStream &is, String &rhs) {
String tmp; Opens up a new class for assigning values
is >> tmp._data;
RHS = tmp;
return is;
}