简体   繁体   English

c ++传递字符串文字而不是const std :: string&?

[英]c++ passing a string literal instead of a const std::string&?

I have the following code which compiles with no warnings (-Wall -pedantic) with g++ 我有以下代码,使用g ++编译没有警告(-Wall -pedantic)

#include <iostream>
#include <string>

using namespace std;

class Foo
{
public:
    Foo(const std::string& s) : str(s)
    { }

    void print()
    {
        cout << str << endl;
    }

private:
    const std::string& str;
};


class Bar
{
public:

    void stuff()
    {
        Foo o("werd");
        o.print();
    }
};


int main(int argc, char **argv)
{
    Bar b;
    b.stuff();

    return 0;
}

But when I run it, only the newline is printed out. 但是当我运行它时,只打印出换行符。 What is going on? 到底是怎么回事?

If I were to do this inside stuff: 如果我要在里面做这件事:

string temp("snoop");
Foo f(temp);
f.print();

then it works fine! 然后它工作正常!

The reason why this fails is because it essentially compiles to the following under the hood. 这种失败的原因是因为它基本上编译成了以下内容。

Foo o(std::string("wurd"));

In this case the Foo value is taking a reference to a temporary object which is deleted after the constructor completes. 在这种情况下, Foo值引用一个临时对象,该对象在构造函数完成后被删除。 Hence it's holding onto a dead value. 因此它保持了死亡价值。 The second version works because it's holding a reference to a local which has a greater lifetime than the Foo instance. 第二个版本有效,因为它持有对具有比Foo实例更长寿命的本地的引用。

To fix this change the memebr from being a const std::string& to a const std::string . 要解决此问题,请将memebr从const std::string&更改为const std::string

Whats happening is that the reference 'str' is being initialized so that it points to the temporary arg, 's'. 发生的事情是,引用'str'正在被初始化,以便它指向临时arg,'s'。 Its pretty much the same as using a pointer - you're counting on the continued existence of your constructor arg, 's'. 它与使用指针几乎相同 - 你依赖于构造函数arg的继续存在,'s'。 When the temporary is deleted (after the constructor ftn returns), then your reference now points at garbage. 删除临时值(在构造函数ftn返回之后),那么您的引用现在指向垃圾。

To fix, change str so that its an actual string object and not a reference. 要修复,请更改str以使其成为实际的字符串对象而不是引用。

const std::string str; const std :: string str;

That way a copy will be made of your arg string, and said copy will have the same lifespan as your Foo object. 这样一个副本将由你的arg字符串组成,并且所述副本将与你的Foo对象具有相同的生命周期。

Extending on the answers given before: If you want to avoid the copy of the data, you can change the member and constructor parameter of Foo to const char* . 扩展之前给出的答案:如果要避免复制数据,可以将Foo的成员和构造函数参数更改为const char*

class Foo
{
public:
    Foo(const char* s) : str(s)
    { }

    void print()
    {
        cout << str << endl;
    }

private:
    const char* str;
};


class Bar
{
public:

    void stuff()
    {
        Foo o("werd");
        o.print();
    }
};


int main(int argc, char **argv)
{
    Bar b;
    b.stuff();

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递给第三方 API 时的 C++ const std:string&amp; security - C++ const std:string& security when passing to a third-party API 将bool传递给const std :: string&的例外情况 - Exception on passing a bool to a const std::string& 将字符串文字传递给一个带有std :: string&的函数 - passing a string literal to a function that takes a std::string& 一个关于c++语法的问题[static const auto compare = [](const std::string&amp; now, const std::string&amp; next) noexcept -&gt;bool ] - A question about c++ grammar [static const auto compare = [](const std::string& now, const std::string& next) noexcept ->bool ] 第一个 C++ 类,不确定某些事情。 特别是“const std::string&amp; 提示 - First c++ class and not sure on some things. specifically "const std::string& prompt 在C ++中,类如何在构造函数中获取const std :: string&参数,但也处理NULL? - In C++, how can a class take a const std::string& parameter in the constructor but also handle NULL? 应该返回const std :: string并返回const std :: string_view吗? - Should methods returning const std::string& return const std::string_view instead? 在 C++17 中使用 const std::string&amp; arguments 是否有意义? - Is there sense in using const std::string& arguments in C++17? C++ 将字符串作为值或 const string&amp; 传递 - C++ Pass a string as a value or const string& 在 C++ 中,如果函数接受 const std::string&amp; 作为输入,我是否可以始终在字符串输入上使用 std::move() 调用? - In C++, if a funcion takes in a const std::string& as input, can I always call with std::move() on the string input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM