普通構造、拷貝構造、移動構造、析構的寫法
阿新 • • 發佈:2018-12-22
class Str { public: char *str; Str(char value[]) { cout << "普通建構函式..." << endl; str = NULL; int len = strlen(value); str = (char *)malloc(len + 1); memset(str, 0, len + 1); strcpy_s(str, len + 1, value); } Str(const Str &s) { cout << "拷貝建構函式..." << endl; str = NULL; int len = strlen(s.str); str = (char *)malloc(len + 1); memset(str, 0, len + 1); strcpy_s(str, len + 1, s.str); } Str(Str &&s) { cout << "移動建構函式..." << endl; str = NULL; str = s.str; s.str = NULL; } ~Str() { cout << "解構函式" << endl; if (str != NULL) { free(str); str = NULL; } } };