开发者

Hiding structure variables from external linkage

开发者 https://www.devze.com 2022-12-28 23:46 出处:网络
Hi can we hide variables in a structure from outside of the given file using static keyword??? similarly can w开发者_开发百科e hide a global variable from outside of the given file using static keywor

Hi can we hide variables in a structure from outside of the given file using static keyword??? similarly can w开发者_开发百科e hide a global variable from outside of the given file using static keyword? if so please let me know how. Thanks in advance...


Use an opaque data type, where the type is declared in a header, but the struct is defined in the implementation. This means any code which includes the header can pass pointers around to the type, but only the implementation can modify and process data contained by the struct.

The typical example is FILE from stdio.h.

For more information see http://en.wikipedia.org/wiki/Opaque_pointer


You can hide a global variable from outside a file using the static keyword, but you cannot hide a subset or a single variable of a structure.


By means of the static keyboard you typically make private a function. If you use it for a variable, both global or local with respect to a function, you obtain a non-reentrant code.

I strongly suggest you to avoid this, because gives you troubles in a multithreaded environment. Also you may not be interested in multithreading programming, but you don't know what you'll need in future!

On your specific issue, I totally agree James Morris advice about opaque pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消