简体   繁体   English

从std :: basic_string取消继承

[英]Un-inherit from std::basic_string

Basically, I have a large project that uses a custom utility class c_string that inherits from std::basic_string<char> . 基本上,我有一个大型项目,它使用从std::basic_string<char>继承的自定义实用程序类c_string For numerous reasons, I would like to edit this class so that 由于种种原因,我想编辑这个类

  1. It does not derive from std::basic_string<char> 它不是从std::basic_string<char>派生的
  2. I do not have to re-implement all the functions 我不必重新实现所有功能
  3. I do not have to touch every file that uses c_string 我不必触及使用c_string每个文件

So I want to change from: 所以我想改变:

class c_string : public std::basic_string<char>
{
public:
    typedef std::basic_string<char> Base;

    c_string() : Base() {}
}

To: 至:

class c_string
{

...

public:

    ...

    c_string() {...}
}

So, I'm wondering if anyone has a good strategy for making this change with minimal impact. 所以,我想知道是否有人有一个很好的策略来做出这个改变,影响最小。

If your class adds custom functionality (that your project needs) over std::string , then you're out of luck: you will either have to encapsulate std::string (and implement all methods to forward to std::string implementation) or inherit from std::string (inheriting from std::string is not a good idea in general). 如果你的类在std::string添加了自定义功能(你的项目需要),那么你就不幸了:你要么必须封装std::string (并实现所有方法转发到std::string实现)或继承自std::string (继承自std::string一般不是一个好主意)。

If your class doesn't add extra functionality over std::string , then replace class c_string { ... } with typedef std::string c_string; 如果你的类没有在std::string添加额外的功能,那么用class c_string { ... } typedef std::string c_string;替换class c_string { ... } typedef std::string c_string; .

There is another thing you can do and that is to change the public inheritance with private inheritance. 您可以做的另一件事就是使用私有继承来更改公共继承。 Doing that, you will get a bunch of compile errors, because all of string's memberfunctions are now private to the clients of your class. 这样做会导致一堆编译错误,因为所有字符串的成员函数现在对您的类的客户端都是私有的。 You can then selectively make these public: 然后你可以有选择地公开这些:

class my_string: std::string {
public:
    typedef std::string base; // convenience
    using base::const_iterator;
    using base::begin;
    using base::end;
};

You should understand private derivation not as "my_string is a std::string" but "my_string is implemented in terms of std::string". 你应该理解私有派生不是“my_string是一个std :: string”,而是“my_string是用std :: string实现的”。 This technique avoids some of the downsides (implicit conversion, slicing etc) of deriving from a type that is not intended to be a baseclass, like std::string. 这种技术避免了从一个不打算成为基类的类型派生的一些缺点(隐式转换,切片等),比如std :: string。 Doing this conversion is easy, it's just work with little risk of breaking anything. 进行这种转换很容易,只需要几乎没有破坏任何东西的风险。 Afterwards though, you have control over the forwarded interfaces, which makes conversion and refactoring much easier. 之后,您可以控制转发的接口,这使得转换和重构变得更加容易。

I can't see any way that you could avoid at least wrapping all the functions. 我看不出任何方法可以避免至少包装所有功能。 The most straightforward way would be to have a private basic_string member, and just write wrappers that call the same function on that member. 最直接的方法是拥有一个私有的basic_string成员,并且只编写在该成员上调用相同函数的包装器。

I have nearly the same problem as zdp. 我有几乎与zdp相同的问题。 Not inherit from std::string leads in a lot of code not to use methods (we have a lot like this) as void Foo(string &s_) where a string is the parameter in the function and the user puts a my_string to Foo(). 在很多代码中不继承std :: string导致不使用方法(我们有很多这样的)作为void Foo(string&s_),其中字符串是函数中的参数,用户将my_string放入Foo( )。 In short therm: Inherit from std::string and you can send your my_string to any function which takes a my_string OR a std::string as parameter. 简而言之:从std :: string继承,您可以将my_string发送到任何以my_string或std :: string作为参数的函数。 I would like to rewrite my my_string class but there are a lot of code with this problem in other hands and a lot of people have to change their code too. 我想重写我的my_string类,但是在其他人手中有很多代码存在这个问题,很多人也不得不改变他们的代码。 One bad choice 14 years ago....... sh.. 一个糟糕的选择14年前....... sh ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM