How do I implement access specifiers in C?
I was trying to implement an access specifier (not sure if this is called an access specifier)
The goal is to make the function (func2) callable in only one place (inside func1).
int func1 ()
{
// Make func2 callable only here`
#define FUNC2_CALLABLE
func2();
#undef FUNC2_CALLABLE
}
#ifdef FUNC2_CALLABLE
int func2 ()
{
return 1;
}
#endif // FUNC2_CALLABLE
func2 should only be called from func1, not anywhere else in your code.
Does the above code match? Any alternative suggestions
<Edit 1>
How to do it this way
int func2()
{
#ifdef FUNC2_CALLABLE
return 1;
#endif
}
Will this work? </ Edit 1>
+3
source to share
3 answers
This will give you the linker error func2 not found (func2 will not be defined if you are using it).
I think you might be looking for statics.
static int fake(int x) { return x * 2; }
int doSomething(int x) { int tmp = fake(x); doOtherThings(); }
The fake won't exist outside of the compilation unit (file mostly).
As far as being able to call a function only from another function, it doesn't make much sense. Just enter the code if that's your ultimate goal.
+1
source to share