Inheriting and overriding functions of a std::string?(继承和覆盖 std::string 的函数?)
问题描述
由于 std::string
实际上是模板化类的 typedef
,我该如何覆盖它?我想制作一个 UTF-8 std::string
它将返回正确的长度等.
Since std::string
is actually a typedef
of a templated class, how can I override it? I want to make a UTF-8 std::string
that will return the correct length, among other things.
推荐答案
DON'T DERIVE FROM STRING
std::string,也就是基本上整个basic_string模板不是设计来派生的.已经有不计其数的文章了.它没有任何虚函数,所以没有什么可以重写的.你能做的最好的事情就是隐藏一些东西.最好是使用组合/聚合!也就是说,只需在您的班级中保留一个字符串类型的成员并转发呼叫!再次,只是为了确保
std::string, that is, basically the whole basic_string template is not designed to be derived from. There are zillions of articles about that already. It doesn't have any virtual functions so there is nothing to override. The best you can do is hide something. Best is to use composition/aggregation! That is, just keep a member of type string in your class and forward the calls! Again, just to make sure
不要从字符串派生
这篇关于继承和覆盖 std::string 的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:继承和覆盖 std::string 的函数?


基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01