Static functions

D

Der Andere

Do non-member static functions exist? If yes, what is the sense of making a
non-member function a static function? Can these functions only access
static variables?

Cheers,
Matthias Treder
 
J

John Harrison

Der Andere said:
Do non-member static functions exist? If yes, what is the sense of making a
non-member function a static function? Can these functions only access
static variables?

Yes they do, but it's a completely different meaning of the keyword static.

A non-member static function cannot be accessed outside of the translation
unit in which it appears. That is the meaning of static when applied to a
function or variable outside of any classes or functions.

A translation unit is all the code in a single compilation, normally this
means one source file and the headers it includes.

john
 
A

Alf P. Steinbach

* Der Andere:
Do non-member static functions exist?

All non-member functions are static.

If yes, what is the sense of making a non-member function
a static function?

The word 'static' has a different effect when applied to a
non-member function.

The non-member function is already a static function.

What 'static' does to that function is give it internal
linkage so that it won't be visible outside the compilation
unit -- i.e., (over-simplified) the linker will not see it.

This use of 'static' is deprecated, and imposes the limitation
that the function cannot be used as a template argument.

The modern way is to instead place the function in an anonymous
namespace, but for convenience or simply out of habit many (including
me) prefer to use 'static'.

Can these functions only access static variables?

Apparently this question does not make sense.
 
R

Rolf Magnus

Der said:
Do non-member static functions exist?
Yes.

If yes, what is the sense of making a non-member function a static
function? Can these functions only access static variables?

No. A static function can only be called from the translation unit it is
defined in. It has internal instead of external linkage, just like
static variables in the same scope do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top