Hidden structure in c

 

Introduction

 

 

Private member variables are support in C++ naturally but this is not the case in C. In this example, I am trying to simulate it with the hidden structure in C.

From the above sequence diagram, you can see that the MessageQ object was defined as a struct in C. Member variables in a struct is public which is accessible from outside of the struct. Please refer to the following code example showing that how I use the hidden structure in C to simulate the private behavior in C++.

Code

Gitlab example

Result

$ ./app/HiddenStruct_C/HiddenStruct_C
thread 002 add number 1000
thread 001 add number 000
thread 001 add number 001
thread 002 add number 1001
thread 001 add number 002
thread 001 add number 003
thread 002 add number 1002
thread 001 add number 004
thread 001 add number 005
thread 002 add number 1003


Comments