|
#include<iostream.h>
#include<string.h>
class prompt
{
private:
char *_temp;
public:
prompt (const char *p);
~prompt ();
const char *getstring ();
};
int
main ()
{
prompt *p_prompt = new prompt ("hi! please enter string : ");
cout << "you entered :" << p_prompt->getstring () << endl;
delete p_prompt;
return 0;
}
prompt:: prompt (const char *p)
{
const int SIZE = 100;
char *temp = new char[SIZE];
cout << p << endl;
cin.getline (temp, SIZE);
//char * //line 28
_temp = new char[strlen (temp) + 1];
strcpy (_temp, temp);
delete[]temp;
}
prompt::~prompt ()
{
delete[]_temp;
}
const char *
prompt::getstring ()
{
return _temp;
}
两个问题:
i)我在gdb中始终看不到temp(print temp),提示 No symbol "temp" in current context;而能看到_temp;
这是为什么?
ii)若把第28行的注释去掉,执行的结果将会不同,会出错。
这又为何?
谢谢! |
|