Hiding private data members? (C++)

妖精的绣舞 提交于 2019-11-28 16:59:58
Kristopher Johnson

The "pimpl" idiom is how this is generally handled.

See

you want to use something like the PIMPL idiom

http://en.wikipedia.org/wiki/Opaque_pointer

The classic way to do this is with a proxy pointer to an internal class which implements the functionality. There's no way to do partial class definitions in C++ that I know of.

Going commercial? ;)

You can create header files, in which you only declare the public and protected API.

The user is only presented with these, which they can include. They link their code with a library, which you built using the complete API and the definitions.

For inlined functions: make sure they are used in non-inlined code, then there will be a definition available in the library (I'm not sure it will be inlined in the user implemenation, however).

For templated code there is no real way around. One half-hearted solution is to make code, which uses the templated code with different object types. The user will be limited to these, because they are the only definitions available in your library.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!