带有char指针的C++类返回垃圾

JMP*_*JMP 3 c++ string pointers

我创建了一个类"Entry"来处理Dictionary条目,但在我的main()中,我创建了Entry()并尝试cout char类型的公共成员,但我得到了垃圾.当我在调试器中查看监视列表时,我看到正在设置的值,但是一旦我访问这些值,就会有垃圾.任何人都可以详细说明我可能会遗失的内容吗?

#include  <iostream>

using  namespace  std;

class Entry
{
    public:
            Entry(const char *line);
            char *Word;
            char *Definition;
};

Entry::Entry(const char *line)
{
    char tmp[100];
    strcpy(tmp, line);

    Word = strtok(tmp, ",") + '\0';
    Definition = strtok(0,",") + '\0';
}

int  main()
{
    Entry *e = new Entry("drink,What you need after a long day's work");
    cout << "Word: " << e->Word << endl;
    cout << "Def: " << e->Definition << endl;
    cout << endl;

    delete e;
    e = 0;

    return  0;
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*l R 11

Word和Definition都指向tmp,它已超出范围,因此包含垃圾.