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 的函数?


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09