继承和覆盖 std::string 的函数? [英] Inheriting and overriding functions of a std::string?

查看:45
本文介绍了继承和覆盖 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 的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆