N
Nagrik
Dear Group,
The book of Bjarne Stroustrup in chapter 5.4.4 says the following
"The word static is one of the most overused words in C and C++. For
static data members it has both of the common meanings: static as in
"statically allocated" as opposed to on the stack or on the free store
and static as in "with restricted visibility" as opposed to with
external linkage. For member functions, static has the second
meaning."
In the same book in r.9.4 it says the following:
"Static members of global class have external linkage (r.3.3). The
declaration of a static data member in its class declaration is not a
definition.
AT r.3.3 it says:
"A name of file scope that is explicitly declared static is local to
its translation unit and can be uses as a name for other objects,
functions, and so on, in other translation units. Such names are said
to hae internal linkage. A name of file scope that is explicitly
declared inline is local to its translation unit..."
/** I am confused about the above mentioned statement (especially of
the phrase "A name of file scope". Can someone explain this) */
In the same section it says:
Static class members have external linkage.
1. By examining all three statements I am confused about the linkage
of static member functions as well as inline functions.
I have the following code
// File a.h
#include "stdafx.h"
#include "iostream.h"
class A {
public:
A() ;
static void someFn();
};
static void indFn();
//File a.cpp
#include "stdafx.h"
#include "a.h"
A::A() {cout << "Constuctor A"<< endl;}
void A::someFn() {cout << " print in static" << endl; }
static void indFn() {cout << " print in static ind" << endl; }
// File staticcheck.cpp
#include "stdafx.h"
#include "iostream.h"
#include "a.h"
int main(int argc, char* argv[])
{
A* a = new A;
a->someFn();
indFn();
return 0;
}
The file staticcheck.cpp only #includes a.h, however, the call to
function /* a->someFn() */ succeeds, although the definition of the
function is defined in a.cpp ( Contrary to internal linkage
directive). At the same time unless I declare and define indFn in a.h
the call to indFn() does not compile. If I declare the indFn in a.h
and define in a.cpp the code does not compile. Hence in this case it
is definitely Internal Linkage.
2. The inline functions also have internal linkage and it makes sense
to define them in each and every module. However, later discussions
give me the impression that compilers now support external linkage for
inline functions. If this is the case then it should obviate the need
to define inline function in every module, wherever it is used.
3. Lastly the static function belongs to the class and not to the
object. Hence, there is only one copy of the function available for
all threads to run. Does it mean if one thread is running the static
function, then that thread will hold a lock on that function and no
other thread can start running the code of that function unless the
other thread is done executing the static function.
4. What happens to local variables (not static variables) declared
inside static function. Do they retain their updated values across
multiple threads, or each thread gets to maintain independent values
for those variables.
Thanks
nagrik
The book of Bjarne Stroustrup in chapter 5.4.4 says the following
"The word static is one of the most overused words in C and C++. For
static data members it has both of the common meanings: static as in
"statically allocated" as opposed to on the stack or on the free store
and static as in "with restricted visibility" as opposed to with
external linkage. For member functions, static has the second
meaning."
In the same book in r.9.4 it says the following:
"Static members of global class have external linkage (r.3.3). The
declaration of a static data member in its class declaration is not a
definition.
AT r.3.3 it says:
"A name of file scope that is explicitly declared static is local to
its translation unit and can be uses as a name for other objects,
functions, and so on, in other translation units. Such names are said
to hae internal linkage. A name of file scope that is explicitly
declared inline is local to its translation unit..."
/** I am confused about the above mentioned statement (especially of
the phrase "A name of file scope". Can someone explain this) */
In the same section it says:
Static class members have external linkage.
1. By examining all three statements I am confused about the linkage
of static member functions as well as inline functions.
I have the following code
// File a.h
#include "stdafx.h"
#include "iostream.h"
class A {
public:
A() ;
static void someFn();
};
static void indFn();
//File a.cpp
#include "stdafx.h"
#include "a.h"
A::A() {cout << "Constuctor A"<< endl;}
void A::someFn() {cout << " print in static" << endl; }
static void indFn() {cout << " print in static ind" << endl; }
// File staticcheck.cpp
#include "stdafx.h"
#include "iostream.h"
#include "a.h"
int main(int argc, char* argv[])
{
A* a = new A;
a->someFn();
indFn();
return 0;
}
The file staticcheck.cpp only #includes a.h, however, the call to
function /* a->someFn() */ succeeds, although the definition of the
function is defined in a.cpp ( Contrary to internal linkage
directive). At the same time unless I declare and define indFn in a.h
the call to indFn() does not compile. If I declare the indFn in a.h
and define in a.cpp the code does not compile. Hence in this case it
is definitely Internal Linkage.
2. The inline functions also have internal linkage and it makes sense
to define them in each and every module. However, later discussions
give me the impression that compilers now support external linkage for
inline functions. If this is the case then it should obviate the need
to define inline function in every module, wherever it is used.
3. Lastly the static function belongs to the class and not to the
object. Hence, there is only one copy of the function available for
all threads to run. Does it mean if one thread is running the static
function, then that thread will hold a lock on that function and no
other thread can start running the code of that function unless the
other thread is done executing the static function.
4. What happens to local variables (not static variables) declared
inside static function. Do they retain their updated values across
multiple threads, or each thread gets to maintain independent values
for those variables.
Thanks
nagrik