How to write this kind of code in C ??
public static Encoding GetEncoding (
int codepage
)
How to write this kind of code in C ??
public static Encoding GetEncoding (
int codepage
)
There is no public in C. Functions have external linkage by default unless you mark them static explicitly. It's a good idea to have a prototype visible for your function (probably in a header file) when using it, so as to avoid mismatched expectations between the caller and reality :-)
For example, the following would be valid C:
encoding.h:
typedef void * Encoding;
Encoding getEncoding (int);
encoding.c:
#include "encoding.h"
Encoding getEncoding (int codePage) {
return 0;
}
If your intent is to convert C++ (or other OO languages) code to C, there are ways to do it such as here, but it's not for the faint of heart :-) Especially if you're supporting the proper OO concepts like inheritance and polymorphism.
There are no formal methods in C, and the static
modifier means something else. There are also no access control modifiers. So:
Encoding *GetEncoding (int codepage) {
}
In java, the return value would probably be an object reference, so in C you would use a *pointer
.
In C, there is no access control and no this
. So public
and static
are already the only way to do things.
You probably want the header (interface) file to contain the prototype
extern Encoding *GetEncoding ( int codepage );
and the source (implementation) file should have
Encoding *GetEncoding ( int codepage ) {
return & masterEncodingList[ codepage ]; /* example implementation */
}
The header file might also define Encoding
:
struct Encoding {
const char *name;
/* other fields */
};
and the source file might define the encodings
Encoding masterEncodingList[] = {
{ "Swahili", /* other fields */ },
{ "Portuguese", /* ... */ },
/* ... */
};
Have, ah, fun!