버그를 찾아 보세요. ^^
글쓴이: gilgil / 작성시간: 화, 2011/09/13 - 8:01오후
#include <iostream>
#include <string>
using namespace std;
#define SAFE_DELETE(A) { if(A != NULL) { delete (A); A = NULL; } }
class MyString
{
public:
string* m_str;
MyString() { m_str = NULL; }
MyString(const char* p) { m_str = new string(p); }
virtual ~MyString() { SAFE_DELETE(m_str); }
};
void write(MyString s)
{
if (s.m_str != NULL)
cout << *s.m_str << endl;
}
int main()
{
MyString s = "test";
write(s);
return 0;
}source code : http://www.gilgil.net/9418
Forums:


우선 복사생성자가 없네요.
gilgil님의 글은 난이도를 짐작하기가 어렵네요.
음... 어렵지는 않는 코드인데요...
상기 코드가 g++에서는 run time error가 나지 않을 수 있는데 main을 다음과 같이 고쳐서 테스트해 보시면 금방 버그가 나올 겁니다. ^^
void test() { MyString s = "test"; write(s); } int main() { test(); return 0; }www.gilgil.net
우선 포인터(m_str)의 주소가 아니라 그 내용을
우선 포인터(m_str)의 주소가 아니라 그 내용을 복사하는 복사생성자가 없네요.
복사생성자를 만들어서 new로
복사생성자를 만들어서 new로 string할당해야겠네요..