473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why we need a function to be static

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
Jul 23 '05 #1
4 1766
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

"Alf P. Steinbach" <al***@start.no> wrote in message
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
Jul 23 '05 #3
* Sharad Kala:

"Alf P. Steinbach" <al***@start.no> wrote in message
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.


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
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.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
3
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
7
by: Chris | last post by:
Hi, I am writing a webpage in C#. Visual Studio .NET. the following code does not provide me with intellisence on the context, session, and page.cache. When I put the large if...then...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
7
by: comp.lang.php | last post by:
<? class EditResultGenerator extends MethodGeneratorForActionPerformer { /** * Create an instance of itself if $this does not yet exist as an EditResultGenerator class object * * @access...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
1
by: christian.bau | last post by:
On Oct 9, 6:49 pm, regis <regis.barbanc...@free.frwrote: C99 Standard draft 6.7.4.3: "An inline definition of a function with external linkage shall not contain a definition of a modifiable...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.