Loading

string参考

#include <iostream>
#include <string.h>
class string { private: char *data; public: string(const char *data = NULL); string(const string& str); ~string(); void print(); int size(); int SetValue(); }; string::string(const char *data ) { if(data == NULL) { this->data = new char[1]; //this->data = '\0'; strcpy(this->data,""); } else { this->data = new char[strlen(data)+1]; strcpy(this->data,data); } } string::string(const string& str) { // std::cout << "拷贝构造函数" << endl; this->data = new char[strlen(str.data)+1]; strcpy(this->data,str.data); } string::~string() { delete []data; } void string::print() { std::cout << data << std::endl; } int string::size() { return strlen(data); } int string::SetValue() { data[0] = 'H'; } int main() { string str; string str1 = str; str1.SetValue(); str1.print(); str.print(); }

 

posted @ 2019-07-16 15:41  砚车干  阅读(135)  评论(0编辑  收藏  举报