private继承的作用

这里有个demo,里面的Stack<T*> : private Stack<void *>,作者对此的解释如下

The partial specialization for other pointer types derives from Stack<void*> privately, since we are
merely using Stack<void*> for implementation purposes, and do not wish to expose any of its interface directly
to the user.

最开始我一直没明白这怎么就对外隐藏了Stack<void *>的interface了?

后来搞明白了,当然,没有直接隐藏Stack<void *>的interface因为其interface暴露在头文件中,准确的说,应该是禁止了把Stack<T*>的instance直接upper转型给Stack<void *>的reference或者pointer

因为如果private继承的话,upper转型是无法编译的,在demo里的代码有体现,这里就不啰嗦了。

点击下载demo

原文地址:https://www.cnblogs.com/qrlozte/p/4386026.html