简体   繁体   中英

How to implement public,private and protected keywords in c-language?

Somebody can give me an example to do this. Suppose static variable scope is limited to file only.That is private to that file.Like that some more examples i want to know. In other words HOW TO ACHIEVE DATA HIDING CONCEPT IN C-LANGUAGE WITH CURRENTLY AVAILABLE KEYWORDS(STRUCT,STATIC...ETC)

This guy is one of the worlds authorities on embedded systems. He wrote this white paper on OOP in c.

http://www.state-machine.com/resources/cplus_3.0_manual.pdf

You can use a private header (say xyz_private.h ) where you define your private structs (say struct xyz_private_t ). In the public header you canthen do

typedef struct xyz_private_t *xyz_ptr_t;    or

typedef struct {
  ... some public members ...
  struct xyz_private_t *private;
} mytype_t;

This is similiar to PIMPL in C++.

private, friend, protected can not be distinguished - either a file can/does access the private header or it can't/doesn't.

You would have to rewrite/extend the compiler, add new grammar to the lexigraphic unit (this is probably the easiest part) and most importantly, add new specs. If you would add these keywords to the C-Language, you wouldn't have the C-Language anymore, but a derivate. Your code wouldn't be understood by any other compiler. Also, those aren't just keywords, they are specific expected behaviour, you can't just implement the keywords, you'd have to add full OOP support to the language. If this is what you are planning - good luck.

Data-hiding is a feature of managed languages, because there is a runtime seperate to your program taking care of things behind the curtain, this does not exist in C. If you want to have these features (data-hiding and other abstractions limited to managed languages), you'd have to exactly produce such a construct, ie. implementing a runtime to take care of things behind the curtain. Then, and ONLY then, are you able to achieve data hiding and other abstractions.

C simply does not offer these possibilites, any code in the program can access any address in the virtual memory of the process, going around anything you are planing.

My advice, even if it will be hard for you: use a managed language for that. C isn't the right language to achieve what you want to do.

Yes, internal linkage (static) effectively makes things private to a file. You don't necessarily need that, though. You can simulate a class with private members by defining a struct inside the source file and providing only a typedef and a set of functions to your users. All of the functions, except for the "constructor" would take an explicit "this" argument. Your faux constructor would simply allocate an instance of your type and return the pointer. Since all your functions would be defined (with external linkage) in the same source as the struct, they can see the members. Your users who only see the typedef and function prototypes cannot.

You can use static in this case to emulate a singleton by having your functions return a pointer to your internally declared instance.

C++ originated as a pre-compiler for C with pre-compilers such as Glockenspiel and CFront. You may find what you are looking for here (it also provide sources).

If you don't pick up clues from there, I think the only way you can achieve true data-hiding in C is to declare your variables as static variables to functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM