我有一个基本上是文本管理器的类。它可以绘制文字和什么。我基本上想要颜色和文本std :: string只是一个常量引用。难道那么没事做保持引用而不是指针?
class TextManager {
const std::string &text;
void draw(const std::string &text) const;
public:
TextManager(const std::string &text)
{
this->text = text;
}
void someMethod()
{
draw(text);
}
};
我想,当拥有的TextManager的文本更改一个实例类,变化体现在TextManager。
使用指针会更好吗? 谢谢
这不会编译,对吧?我不认为你可以改变一个const引用。 – EboMike 2010-10-29 00:10:44
我只是写了这个给我的概念的想法。 – jmasterx 2010-10-29 00:11:57
这段代码几乎肯定不是你想要的。 'TextManager m(“asd”); m.someMethod(); // UB'。你应该存储一个'std :: string'。 – GManNickG 2010-10-29 00:40:32