Where to put the 'static' modifier ?

Hi there!

I'm a little confused about the 'static' modifier. Where I have to put this word ?

These tree codes are correct:

1
2
3
4
5
6
static void f();
void f();
void f()
{
	//...
}

1
2
3
4
5
6
static void f();
static void f();
static void f()
{
	//...
}

1
2
3
4
5
6
static void f();
void f();
static void f()
{
	//...
}


I think that the general rule is:

The 'static' word have to be placed in the first function prototype. After that - it's neutral.

Am I right ?
Last edited on
The 'static' keyword is one of the most overloaded in C++. What exactly are you trying to do with it?

If I understand you correctly, you want to have a module-local function (one that does not appear in linker export clauses), then you should be as explicit as possible:

1
2
3
4
5
6
7
8
static void f();  // a prototype

//... other stuff here

static void f()  // the definition
{
	//...
}

Personally, I don't like extraneous prototypes lying around if I can avoid them. If I have a module-locale function, I define it before externally visible functions.

You can read more about the static keyword here:
http://www.cprogramming.com/tutorial/statickeyword.html

Hope this helps.
Duoas wrote:
then you should be as explicit as possible


Yes, I know - it's the clearest way :)

But my little "rule" is correct, isn't it ?
File statics are a C holdover. You can use an unnamed namespace instead. (IIRC.)
Last edited on
I know :)

I'd like to know whether my little "rule" is correct ;) That's all...
It's just about probably.
Topic archived. No new replies allowed.