重写std::string的赋值(=)操作符
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
函数的调用
相关文章:
- 使用std::enable_if元函数重载模板类中的操作符+=
- Std::initializer_list和=操作符
- 使用比较操作符将Std::string作为Std::map中的键
- Cxx-prettyprint(用于标准容器)在命名空间std中定义了它的输出操作符——这违反了标准吗?
- 重写std::string的赋值(=)操作符
- std::transform中的二元操作符,带unique_ptr的vector对象
- 如何创建一个std::map的常数值,它仍然可以被[]操作符访问
- 带有std::index_sequence的三元操作符
- 在继承std::vector的类中使用操作符[]
- 使用c++中的流操作符解析std::uint类型
- c++ std::map::iterator一无所获,但下标操作符返回对映射值的引用
- 为什么std:: bitset::参考::操作符~
- ' std::bitset::reference '赋值操作符
- c++中的宏操作符和std::字符串比较
- 重载操作符来处理我自己的类作为std类是一个好做法吗?
- 用new操作符定义std::shared_ptr时出错
- 清除std::vector对象需要赋值操作符.为什么
- std::shared_future操作符=线程安全性/原子性
- 为什么std::weak_ptr没有move构造函数或move赋值操作符?
- 访问由std::shared_ptr封装的类的操作符重载