3

I was trying to implement access specifier (not sure if that is called access specifier)

The purpose is to make a function (func2) callable only at 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 be callable only from func1 and not from any other place in the code.

Does the above code serve the purpose ? Any alternative suggestions

< Edit 1 >

How about doing it this way

 int func2() 
 { 
#ifdef FUNC2_CALLABLE 
 return 1; 
#endif 
 } 

Will this work ? < / Edit 1>

Bleamer
  • 637
  • 9
  • 24
  • It doesn't work that way. Only thing you can do is not to export it, declare fun2() as static in func2(), then it is not visible to the linker and cannot be called outside of this compile unit. – pizza Apr 01 '12 at 03:27
  • 3
    http://stackoverflow.com/questions/1401781/how-to-implement-a-private-restricted-function-in-c – Flot2011 Apr 01 '12 at 03:36
  • 2
    if it's only callable in one place, and you don't want other people to be able to call it - why make it a function at all? why not put the code directly in func1()? – Mike Corcoran Apr 01 '12 at 03:37
  • @Flot2011 - I was definitely thinking, "I remember answering a question like this a while back." Thanks for digging it up for me. – Chris Lutz Apr 01 '12 at 04:40
  • @pizza how about doing it this way int func2() { #ifdef FUNC2_CALLABLE return 1; #endif } Will this work ? – Bleamer Apr 03 '12 at 03:49
  • 1
    It won't work with preprocessing tricks. Remember preprocessing happens before compile. – pizza Apr 03 '12 at 04:03

3 Answers3

1

That will give you a linker error of func2 not found (func2 won't be defined if you use that).

I think you might be looking for static.

static int fake(int x) { return x * 2; }

int doSomething(int x) { int tmp = fake(x); doOtherThings(); }

The fake would not exist outside of the compilation unit (file basically).

As for allowing a function to only be called from another function, that doesn't make much sense. Just inline the code if that's your end goal.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • It would have been ok to put inline code had it been a small chunk of code, but what if i need to use this in multiple instances ? – Bleamer Apr 03 '12 at 04:00
  • @Bleamer In that case you're stuck using a static function. That will restrict it to the compilation unit, but really wallyk's answer is the better option. That further restricts the scope of the function. Though really, as long as the function doesn't leave the compilation unit, you shouldn't have anything to worry about. Is there some particular reason you need to hide it more than that? (Chris Lutz suggestion is quite interesting too -- it could very specifically limit the scope of a function's declaration) – Corbin Apr 03 '12 at 04:20
  • The legacy source code that I work on is full of arbitrary calls to low level APIs, developers do not set flags correctly after calling them, so my idea was to keep them from calling these functions out of turn. Thanks for your help. – Bleamer Apr 05 '12 at 14:20
0

There is no real way to do it. The best that can be done in standard C is to make func2 static and define it close to the bottom of the source file:

static int func2 ()
{ 
  return 1;
}

int func1 ()
{
   func2(); 
}

(end of file)
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Technically, you can define `func2()` anywhere, so long as once you're done you do something like `#define func2() $&@*^%|` when you need it to go "out of scope." – Chris Lutz Apr 01 '12 at 04:53
  • @ChrisLutz: Interesting approach. I'll file that away in my bag of tricks next time I need to see if something similar is a problem. – wallyk Apr 01 '12 at 05:51
0

Maybe you can use static keyword .

But the function with static is accessible for all the code in the same file. Other files cannot access it.

llj098
  • 1,404
  • 11
  • 13
  • "Method" and "members" aren't really helpful words in C. I know what you mean, but it's easy for a newcomer to get mixed up in OO terminology in a language that doesn't natively support OO. – Chris Lutz Apr 01 '12 at 04:52