C ++中的临时问题

时间:2011-04-12 11:40:16

标签: c++ boost

我有这段代码使用Boost.Filesystem打印目录的内容:

class Shell {
private:
    const string& command;
    const path& firstPath;
    const path& secondPath;
public:
    // constructor
    Shell(const string& _command, const path& _firstPath, const path& _secondPath = path()): command(_command), firstPath(_firstPath), secondPath(_secondPath) {}

    //destructor
    ~Shell() {}

    //execute commands
    void executeCommand()
    {
        if(command.compare("ls"))
        {
            ls();
        }
    }

    void ls()
    {
        if(exists(firstPath)) 
        {
            vector<path> vecPath;

            copy(directory_iterator(firstPath), directory_iterator(), back_inserter(vecPath));
            sort(vecPath.begin(), vecPath.end());
            for(vector<path>::iterator it = vecPath.begin(); it != vecPath.end(); ++it)
            {
                cout << *it << endl;
            }
        }
    }
};


int main(int argc, char** argv) 
{
    path Path = "c:\\workspace";
    Shell shell("ls", Path); // GOOD
    // Shell shell("ls", "c:\\workspace");  BAD creates temporary !!
    shell.executeCommand();
}

如果我将第二个参数direclty作为const char发送*我知道它会创建一个临时的,我的构造函数参数只会在构造函数中可见,然后它会丢失,因为它引用了临时值。
我的问题是,为什么第一个参数,即字符串发生同样的事情,因为我也将它作为一个const char *直接发送给他,但是值不会在构造函数之外丢失?

3 个答案:

答案 0 :(得分:5)

Shell shell("ls", Path); // BAD - just happens to work as you expected.

未定义的行为 - 任何事情都可能发生,包括按照您的期望行事。临时分配的内存恰好在你的情况下没有被覆盖。

答案 1 :(得分:2)

std::string *ref;

{ 
    std::string tmp("temp");
    ref = &ref;  // risky, need to keep lifetime of tmp in mind

    const char* const rawptr = ref->c_str(); // equivalent to tmp.c_str()

    tmp = "altered"; // *ref also modified, rawptr completely invalid
}
// *ref _and_ rawptr completely invalid.

答案 2 :(得分:1)

您存储引用而不是副本的具体原因是什么? 通过改变你的班级

class Shell {
private:
    const string& command;
    const path& firstPath;
    const path& secondPath;
...

class Shell {
private:
    const string command;
    const path firstPath;
    const path secondPath;
...

你可以避免很多问题,这两种情况都是正确的。 为什么会出现这种情况,你可以在sehe的答案中看到