完整String.h:
#ifndef __MYSTRING__
#define __MYSTRING__
class String {
private:
char* m_data;
public:
String( );
String(const char* cstr);
String(const String& str);
String& operator=(const String& str);
~String();
char* get_c_str() const { return m_data; }
};
inline
String::String( ) {
m_data = new char[1];
*m_data = '\0';
}
inline
String::String(const char* cstr) {
if (cstr) {
m_data = new char[strlen(cstr) + 1];
strcpy(m_data, cstr);
}
else {
m_data = new char[1];
*m_data = '\0';
}
}
inline
String::~String() {
delete[] m_data;
}
inline
String::String(const String& str) {
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
}
inline
String& String::operator=(const String& str) {
if (this == &str) {
return *this;
}
delete[] m_data;
m_data = new char[ strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
return *this;
}
#include <iostream>
ostream& operator << (ostream& os, const String& str) {
os << str.get_c_str();
return os;
}
#endif
测试代码main.cpp
#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;
#include "String.h"
int main() {
String s1();
String s2("hello");
String s3(s2);
String* p = new String("hello");
std::cout << *p << std::endl;
system("pause");
return 0;
}
|