继承和覆盖 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
不要从字符串派生
相关文章