C ++中的void指针的缺点 [英] drawbacks of a void pointer in C++
问题描述
就我所知,C ++中的一个void指针 void *
可以指向任何东西。这可能是非常有用的(对我来说)如果我想开发一个解决方案,而不使用某种继承。但是我想知道的是,这种方法是否有任何性能上的缺点?
A far as I know, a void pointer in C++ void*
can point to anything. This might be quite useful (for me) if I want to develop a solution without using some sort of inheritance. But the question I want to know is whether there is any performance drawbacks in this approach?
推荐答案
最大的缺点是,的void指针阻止编译器执行类型检查。特别是在支持面向对象原则的语言中,我认为大量使用void指针是很奇怪的。
The biggest drawback is that making use of void pointers stops the compiler from being able to enforce type checking. Especially in a language that supports object oriented principles, I think it would be strange to be making heavy use of void pointers.
你更可能在C中找到void指针以模仿一些多态行为,但存在相同的类型安全问题。
You are more likely to find void pointers in C to mimic some polymorphic behavior, but the same type-safety concerns exist.
这篇关于C ++中的void指针的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!