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

查看:789
本文介绍了你不能继承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 的其余接口。好吧,作为一个守法的公民,我的第一个想法是在中有一个 std :: vector 成员类。但是,我将不得不手动reprovide所有的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 :)

推荐答案

实际上,<$ c的公共继承没有问题$ c> 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 :: vector MyVector 之间有什么区别?哪一个最好在这里和那里使用?如果我需要将 std :: vector 移动到 MyVector ,该怎么办?我可以只使用 swap()吗?

The problem is that MyVector is a new entity. It means a new C++ developer should know what's 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天全站免登陆