我正在尝试制作一个具有 SimpleWindow 类的 gui,其中包含 textPanel 类:
class textPanel{
private:
std::string text_m;
public:
textPanel(std::string str):text_m(str){}
~textPanel();
};
class SimpleWindow{
public:
SimpleWindow();
~SimpleWindow();
textPanel text_panel_m;
};
SimpleWindow::SimpleWindow():
text_panel_m(std::string temp("default value"))
{
}
我希望能够使用转换为 std::string 的 const char* 初始化 text_panel_m,而不需要创建另一个采用 const char* 的构造函数。我是否应该创建另一个以 const char* 作为参数的构造函数?如果我这样做,有没有办法减少使用 c++0x 的冗余构造函数代码量?
使用上述方法,我在初始化 text_panel_m 成员变量时遇到困难。 g++ 给我以下错误:
simpleWindow.cpp:49: error: expected primary-expression before ‘temp’
simpleWindow.cpp: In member function ‘bool SimpleWindow::drawText(std::string)’:
如何在不使用默认构造函数的情况下初始化 text_panel_m 成员变量?
你快到了:
SimpleWindow::SimpleWindow():
text_panel_m("default value")
{
}
应该解决这个问题,使用std::string
的隐式转换构造函数const char*
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)