65,208
社区成员
发帖
与我相关
我的任务
分享#include <vector>
#include <iostream>
using namespace std;
int i = 0;
int j = 0;
class CDemo{
public:
CDemo():str(NULL){
cout << "constructor:" << i++ << endl;
}
CDemo(const CDemo& cd)
{
cout << "copy constructor:" << i++ << endl;
this->str = new char[strlen(cd.str) + 1];
strcpy(this->str, cd.str);
}
~CDemo(){
cout << "destructor:" << j++ << endl;
if(str)
{
delete [] str;
str = NULL;
}
}
char* str;
};
void main()
{
CDemo d1;
d1.str = new char[32];
strcpy(d1.str, "trend micro");
vector<CDemo> *a1 = new vector<CDemo>();
a1->push_back(d1);
delete a1;
a1 = NULL;
getchar(); // 输入s
return;
}
#include <vector>
#include <iostream>
using namespace std;
int i = 0;
int j = 0;
class CDemo{
public:
CDemo():str(NULL){
cout << "constructor:" << i++ << endl;
}
//CDemo(const CDemo& cd)
//{
// cout << "copy constructor:" << i++ << endl;
// this->str = new char[strlen(cd.str) + 1];
// strcpy(this->str, cd.str);
//}
~CDemo(){
cout << "destructor:" << j++ << endl;
if(str)
{
delete [] str;
str = NULL;
}
}
char* str;
};
void main()
{
CDemo d1;
d1.str = new char[32];
strcpy(d1.str, "trend micro");
vector<CDemo> *a1 = new vector<CDemo>();
a1->push_back(d1);
delete a1;
a1 = NULL;
getchar(); // 输入s
return;
}