What is "variable protection" in C? How it works?

In C ++, we have abstraction and data hiding. Can we achieve this through C?

+3
source share
5 answers

Define the structure in the .C file, and only “forward the announcement” the structure in its header.

Therefore, your .C file may contain the following:

struct Car
   {
   char *brand;
   int   maxspeed;
   };

And your .H file may contain the following:

typedef struct Car *CarHandle;

Then write down the functions for driving the car (setters, getters, ...) and place them in the same .C file as in the case when the structure is defined. Of course, function prototypes should be placed in the header.

Callers can now use CarHandle and functions that run on CarHandle but never see what's inside the Car structure.

+6

, " " ++. .

+2

() , .

0

, , ( , , , ).

. static . , static .

, . .

0
source

High and Low-Level C contains a lot of good tips. In particular, check out the Abstract Data Types section .

See also: What are the modulation methods for C code?

0
source

Source: https://habr.com/ru/post/1748290/


All Articles