请高手路过侃侃,突然遇到的郁闷问题#includeiostream#includecstringusingnamespacestdclasstest{pub
请高手路过侃侃,突然遇到的郁闷问题
#include <iostream>
#include <cstring>
using namespace std;
class test
{
public:
test(int i,const char *str):m_i(i)
{
this-> m_str=new char[strlen(str)+1];
strcpy(this-> m_str,str);
}
~test()
{
std::wcout < < this < < std::endl;
delete []this-> m_str;
}
void func(test t)
{
std::wcout < < &t < < std::endl;
return;
}
private:
int m_i;
char *m_str;
};
int main()
{
test test1(10, "abc ");
test test2(10, "abc ");
std::wcout < < &test2 < < std::endl;
test1.func(test2);
cin.get();
}
在test1.func返回的时候形参t析构delete []t.m_str的同时,会把test2.m_str也delete了?
[解决办法]
请增加正确实现了深拷贝的拷贝构造函数。
