我真的不是说要覆盖它,因为我知道这是不可能的(除非我自己做)。但是我该怎么做才能像这样
strText = "bla bla";
strText.Compile(); //<--- I want this one to be implicitly call.
我知道我可以用这样的方法做到这一点
updateText(const std::string& text)
{
strText = text;
Compile();
}
或
std::string& updateText()
{
Compile(); //hmm not sure about this. never try
return strText;
}
但是有没有其他技术可以通过只做来隐式地实现这一点
strText = newText; //<--automatically call the `Compile()`
??
在我放弃 updateText()
谢谢!
最佳答案
我能想到的唯一解决方案是定义 my::string
(在内部存储一个 std::string
以提供基本的字符串功能)并定义:
my::string(const char*);
为了允许从 C 样式字符串和 my::string
进行隐式转换,然后定义:
my::string& operator=(const char*);
实现对Compile
函数的调用。
关于C++ 覆盖 std::string 的赋值 ( = ) 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788837/