I have two questions, I would like to ask:
1) at the moment im refactoring my code and i have 6 functions, which share one function in common, exp.:
< pre> CodeSelect all;
static void bar(INT32 x) { }
void foo1() { bar(1); }
void foo2() { bar(2); }
now my question, is there a trick, i could put foo1 and foo2 in seperate .c files and keep bar still "static" thus invisible to outside linking?
is there a trick, i could put foo1 and foo2 in seperate .c files and keep bar still "static" thus invisible to outside linking?
Yes, there is. Put the definition of bar() into a header file and include it in both .c files (that contain foo1() and foo2()). The code for bar() will likely be generated twice, but that's a different matter.
bar.h:
#ifndef BAR_H #define BAR_H typedef long INT32; typedef void (*fp_t)(INT32); extern fp_t bar; #endif
bar.c:
#include "bar.h" extern void foo1(void), foo2(void); static void bar_(INT32); fp_t bar = bar_; int main(void) { foo1(); foo2(); return 0; } static void bar_(INT32 i) { (void)i; }
foo1.c:
#include "bar.h" void foo1(void) { bar(1); }
foo2.c:
#include "bar.h" void foo2(void) { bar(2); }
Expose the static function through a function pointer? Nice.
Well, exposing the static function through a function pointer is quite similar to what C++ does when you call a virtual method.
But this is a situation where a question will lead to bad answers, since the original question is a request to use a specific keyword and then also wanting code that should ignore that keyword. It's a bit like "how can I create a const variable that I can also modify".
So should all questions get an answer, even when the answer will have to be questionable?
I have got the best solution. Discussion done.