你不能从 std::vector 继承 [英] Thou shalt not inherit from std::vector

查看:32
本文介绍了你不能从 std::vector 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这真的很难承认,但我现在确实有强烈的诱惑要继承std::vector.

Ok, this is really difficult to confess, but I do have a strong temptation at the moment to inherit from std::vector.

我需要大约 10 个自定义向量算法,我希望它们直接成为向量的成员.但自然我也想拥有 std::vector 接口的其余部分.好吧,作为一个守法公民,我的第一个想法是在 MyVector 类中有一个 std::vector 成员.但随后我将不得不手动重新提供所有 std::vector 的接口.打字太多了.接下来,我考虑了私有继承,这样我就不会重新提供方法,而是在公共部分编写一堆使用 std::vector::member 的 .这其实也很乏味.

I need about 10 customized algorithms for vector and I want them to be directly members of the vector. But naturally I want also to have the rest of std::vector's interface. Well, my first idea, as a law-abiding citizen, was to have an std::vector member in MyVector class. But then I would have to manually reprovide all of the std::vector's interface. Too much to type. Next, I thought about private inheritance, so that instead of reproviding methods I would write a bunch of using std::vector::member's in the public section. This is tedious too actually.

在这里,我确实认为我可以简单地从 std::vector 公开继承,但在文档中提供警告,不应以多态方式使用此类.我认为大多数开发人员都有足够的能力理解无论如何都不应该多态地使用它.

And here I am, I really do think that I can simply inherit publicly from std::vector, but provide a warning in the documentation that this class should not be used polymorphically. I think most developers are competent enough to understand that this shouldn't be used polymorphically anyway.

我的决定绝对没有道理吗?如果是这样,为什么?你能提供一个替代方案,让额外的成员实际上成为成员,但不会涉及重新输入 vector 的所有界面吗?我对此表示怀疑,但如果可以,我会很高兴.

Is my decision absolutely unjustifiable? If so, why? Can you provide an alternative which would have the additional members actually members but would not involve retyping all of vector's interface? I doubt it, but if you can, I'll just be happy.

此外,除了一些白痴可以写出类似的东西

Also, apart from the fact that some idiot can write something like

std::vector<int>* p  = new MyVector

使用 MyVector 是否还有其他现实危险?通过说现实,我放弃了想象一个带有指向向量的指针的函数......

is there any other realistic peril in using MyVector? By saying realistic I discard things like imagine a function which takes a pointer to vector ...

好吧,我已经陈述了我的情况.我犯罪了.现在由你决定是否原谅我:)

Well, I've stated my case. I have sinned. Now it's up to you to forgive me or not :)

推荐答案

其实std::vector 的公共继承并没有什么问题.如果你需要这个,那就去做吧.

Actually, there is nothing wrong with public inheritance of std::vector. If you need this, just do that.

我建议只有在确实有必要时才这样做.只有当你不能用自由函数做你想做的事时(例如应该保持一些状态).

I would suggest doing that only if it is really necessary. Only if you can't do what you want with free functions (e.g. should keep some state).

问题在于 MyVector 是一个新实体.这意味着新的 C++ 开发人员在使用它之前应该知道它到底是什么.std::vectorMyVector 有什么区别?哪一个更适合在这里和那里使用?如果我需要将 std::vector 移动到 MyVector 怎么办?我可以只使用 swap() 还是不使用?

The problem is that MyVector is a new entity. It means a new C++ developer should know what the hell it is before using it. What's the difference between std::vector and MyVector? Which one is better to use here and there? What if I need to move std::vector to MyVector? May I just use swap() or not?

不要仅仅为了让某些东西看起来更好而产生新的实体.这些实体(尤其是如此常见的实体)不会生活在真空中.它们将生活在熵不断增加的混合环境中.

Do not produce new entities just to make something to look better. These entities (especially, such common) aren't going to live in vacuum. They will live in mixed environment with constantly increased entropy.

这篇关于你不能从 std::vector 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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