之前在 main 函数里,赋值 6 个空格给一个变量可以执行
int main()
{
string sidebar(6, ' ');
return 0;
}
之后在类里面
class somethig
{
public:
void do_something()
{
sidebar(6, ' ');
// 或者是
sidebar = sidebar(6, ' ');
// 都出错
}
private:
string sidebar;
};
g++ 编译后就会出错
对‘(std::string {aka std::basic_string
有没有像 sidebar(6, ' ')
这样简洁实现跟下面一样效果的代码?
sidebar = " "; // 六个空格
Or as shown in 1, initialize member variables in the constructor. This feature is called "constructor initialization list".
Or assign it a value in a member function, as shown in 2.
The poster needs to understand the difference between C++ object copying and assignment, as well as the detailed differences in syntax