#include <iostream> using namespace std; #pragma warning(disable:4996) class Person { public: Person(char *pN) { cout << "Constructing " << pN << endl; pName = new char[strlen(pN) + 1]; printf("%p\n", pName); if (pName != 0) { strcpy(pName, pN); } } /* Person(const Person& p) { cout << "copying " << endl; pName = new char[strlen(p.pName) + 1]; if (pName != 0) strcpy(pName, p.pName); printf("%p\n", pName); } */ ~Person() { printf("%p\n", pName); cout << "Destructing " << endl; //pName[0] = '\0'; delete[] pName; } protected: char *pName; }; int main() { Person p1("Randy"); Person p2(p1); //即Person p2 (p1); return 0; }
#include <iostream> using namespace std; #pragma warning(disable:4996) class Person { public: Person(char *pN) { cout << "Constructing " << pN << endl; pName = new char[strlen(pN) + 1]; printf("%p\n", pName); if (pName != 0) { strcpy(pName, pN); } } Person(const Person& p) { cout << "copying " << endl; pName = new char[strlen(p.pName)+1]; if (pName != 0) strcpy(pName, p.pName); printf("%p\n", pName); } ~Person() { cout << "Destructing " << endl; //pName[0] = '\0'; delete[] pName; } protected: char *pName; }; int main() { Person p1("Randy"); Person p2(p1); //即Person p2 (p1); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u010003835/article/details/47314811