继承和覆盖 std::string 的函数?

Inheriting and overriding functions of a std::string?(继承和覆盖 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 的函数?

基础教程推荐