[英]C++ Overriding assignment ( = ) operator of std::string
我真的不是要覆蓋它,因為我知道這是不可能的(除非我自己做)。 但是我該怎么做
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
函數的調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.