By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,977 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,977 IT Pros & Developers. It's quick & easy.

question about 'static' definitions

P: n/a
Hello, All!

As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it declared. So,
my question is: where can it be used in real applications?

Thanks in advance.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Roman Mashak wrote:
Hello, All!

As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it declared. So,
my question is: where can it be used in real applications?


In every single translation unit.

For example, if you decide to have essentially "one function per module"
then this usually means that you have one "visible" function, i.e.
a function with external linkage which can be called from "everywhere".
Now, this function may become too large or may contain non-portable
parts, so you split off other functions. These functions reside within
the same translation unit but usually are not useful out of context or
should not be accessible from without as this would enable easy misuse
of the module. So, you do not want these functions to be "visible".
Using 'static' gives them internal linkage, et voila.
Another point are identifiers -- if you do not broadcast the name of
all functions into the global namespace, then you are less likely
to run into name clashes. This is, for example, important for
libraries; all the functions not intended for the user should not
pollute the namespace.
This becomes obvious if there are two libraries which both have the
externally useless function my_little_helper() and give you trouble
as they cannot be used together reliably.

So, the question really is: When do you _not_ want your functions to
have internal linkage?
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2

P: n/a
Roman Mashak wrote:

As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it
declared. So, my question is: where can it be used in real
applications? From within the file within which it is declared. No prototype

should appear in the corresponding .h header.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Nov 14 '05 #3

P: n/a

"Roman Mashak" <mr*@tusur.ru> wrote

As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it declared.
So, my question is: where can it be used in real applications?

It very frequently happens that you only want to expose certian functions to
the outside world.
For instance one of my files does clustering. It uses the kmeans algorithm,
so I want the caller to be able to call the function called kmeans().
Internally the algorithm needs a cluster structure that conints a list of
members and centroids, which the caller doesn't need to know about. Also, it
doesn't make sense for him to call any of the subroutines directly.
By making the subroutines "static" I can use a simple name like "assign"
without any danger of a name clash, and also I know that I can modify the
functions to my heart's content, as long as I still provide a list of
clusters for the call to kmeans(), without breaking anything else.
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.