why we need a function to be static

J

Jianli Shen

New c++:)

When look through code, I always find lot of funtions are
declared as (in .h):
static void function1();
when implementing,(in .cpp), just
void function1(){

}




It will be nice to explain the art of usage for static member, extern member
(no extern function)etc.

Thanks
 
A

Alf P. Steinbach

* Jianli Shen:
New c++:)

When look through code, I always find lot of funtions are
declared as (in .h):
static void function1();
when implementing,(in .cpp), just
void function1(){

}

Perhaps that description is not quite accurate, because such code is
meaningless.

It will be nice to explain the art of usage for static member, extern member
(no extern function)etc.

A member is something that's part of a class.

Your 'function1' example is not a member function.

The word 'static' means one thing for a free-standing function, and something
else for a member function.

For a free-standing function 'static' denotes internal linkage, i.e. a
function that is not visible outside this compilation unit. That usage is
deprecated. The new feature replacing that usage is the anonymous namespace.
The word 'extern' denotes the opposite of 'static' in this context. Etc. I
don't know, there's nothing in between internal and external linkage.

For a member function 'static' denotes a member that conceptually is
free-standing, but is within the scope of the class. Thus, a static member
function can be called without having an instance of the class. And it's got
access to static data members in the class.
 
S

Sharad Kala

Alf P. Steinbach said:
Etc. I don't know, there's nothing in between internal and external
linkage.

To OP: Apart from internal and external linkage there is concept of no
linkage also. When a name has no linkage, the entity it denotes cannot be
referred to by names from other scopes.

Sharad
 
A

Alf P. Steinbach

* Sharad Kala:
linkage.

To OP: Apart from internal and external linkage there is concept of no
linkage also. When a name has no linkage, the entity it denotes cannot be
referred to by names from other scopes.

Thanks, yes, that applies to member functions of a local class (a class
defined within a function), so it is relevant.

Internal linkage might be viewed as a restriction of external linkage, and no
linkage might then be viewed as a restriction of internal linkage, a so severe
restriction that no linkage (possibility of declaring a name that stands for
the same) remains.

There is no special keyword for denoting no linkage: you get no linkage for
certain kinds of entities in certain contexts, most notably within a function.
 
E

Evan

There are three uses of static in C++.

Alf Steinback described the first two above: designating internal
linkage and making a static member function.

The first, designating internal linkage, I just want to add a comment I
think clarifying something Alf said, which is that extern and this
sense of static are opposite. While this is sorta true at a high level,
when looking at even a reasonably detailed level IMO the analogy breaks
down. The keyword extern imports a variable that is declared in another
cpp file. Static *prevents* what it's modifying from being imported in
another cpp file.

For example:
a.cpp:
int my_var;
static int my_static_var;

b.cpp:
extern int my_var; // refers to the same variable as my_var in
a.cpp
extern int my_static_var; // no can do; my_static_var has internal
linkage

Better version of a.cpp:
int my_var;
namespace
{ int my_static_var; }

The same works for functions.


Class members can also be static:
struct myClass
{
static int static_var;
static void static_foo() { }
int var;
void foo() {}
};

[later]

myClass my_object;
my_object.var = 5;
my_object.foo();
my_object.static_foo(); // compiles on VC++ 7... is this right?
my_object.static_var = 5; // no can do... static_var isn't a member
of my_object...

myClass::static_var = 5; // ...only the class itself
myClass::static_foo(); // works
myClass::foo(); // nope; foo() is an instance member (ie not
sattic)


The third use is for local variables in functions to keep their values
between calls:

void f()
{ static int i=0;
cout << i++;
}

int main()
{ f(); f(); f();
}

Prints 012.
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top