472,973 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

linkage of static and inline functions

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

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

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 {
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;

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.


Nov 27 '07 #1
2 3730
1. Not sure what is the question here.
The static member of a class has external linkage (9.4)
In your example it is A::someFn
the static function (which is a not a member of any class ) has an
internal linkage (3.3)

you have a forward declaration of 'indFn' in a.h and the definition of
'indFn' in a.cpp.
But since it is declared status it has internal linkage and visible
only inside the translation(or compilation) unit. (It is legal to
define 'indFn' also in staticcheck.cpp).

3. >the static function belongs to the class and not to the object.
Correct. Meaning, you can call the function on class, not on the
>Hence, there is only one copy of the function available for all threads to run.
Not sure what do you mean. The CODE segment is always shared for all
The static function, as any other function, can be called from several
threads simultaneously.

4. While threads share code segment, data segment and heap, each
thread has its own stack.
That's why local variable never require synchronization, only global/
static data can be accessed from several threads simultaneously.
>What happens to local variables (not static variables) declared inside static function.
They are not preserved between calls (not from the same thread, nor
from different ones)


Nov 27 '07 #2
nagrik wrote:
From your reply I follow;

1. Static member functions inside a class have external linkage. And
will have single copy of
2. Static global functinos (not belonging to any
class) have internal linkage
and will have multiple
copies of codes with different addresses in an
Well they _may_ have multiple copies. They _will_ have multiple copies
if you take explicit steps to create these copies. Like put the
function's definition into the header file and include it into several
translation units. Or manually copy the definition of the function into
several translation units.

Best regards,
Andrey Tarasevich
Nov 28 '07 #3

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

Similar topics

by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html# there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.