【发布时间】:2021-01-07 04:51:54
【问题描述】:
我想使用 lambda 表达式初始化一个类成员 (std::string filePath)。
程序编译正常,但没有输出。这里有什么问题?
#include <iostream>
#include <string>
class MyString
{
public:
std::string filePath = [this] ()
{
return oneStr.append(" two");
}();
std::string oneStr;
};
int main()
{
MyString str;
str.oneStr = std::string(" one");
printf("%s", str.oneStr.c_str());
}
【问题讨论】:
-
当
filePath被初始化时,我认为你不能假设oneStr已经被构造了。 -
尝试颠倒您的成员的顺序
-
您的预期输出是什么?
" one two"为oneStr? -
@Soumyajit Roy 我在编译后发布了更新。