C namespace using structures

You can simulate namespaces in C as follows:

#include <stdio.h>
#include <math.h>

struct math_namespace {
    double (*sin)(double);
};
const struct math_namespace math = {sin};

int main() {    
    printf("%f\n", math.sin(3));

    return 0;
}

Are there any flaws in this, or just a situation where the prefix makes more sense? It just seems that doing it so clean.

+5
source share
4 answers

This method is already used in real projects such as C Containers Library from Jacob Navia. C is not intended for object oriented programming. This is not very efficient, since you need to (1) access the structure and (2) dereference the function pointer. If you really need prefixes, I think changing your identifiers remains the best solution.

+3
source
+1

? , , , .

" ", . ?

0

, , , sin. , .

, ELF. , . , . , ELF, , . Windows , .

0

All Articles