John Humphreys
John Humphreys

Reputation: 39294

What storage class is a static inside a function and why?

If I'm using a singleton pattern (yeah, I know - they're usually bad)... and I had an instance function like so:

MySingleton* Instance() {
    static MySingleton instance;
    return &instance;
}

what storage class is instance?

I'm reading "Programming With POSIX Threads by David R. Butenhof", and I came across text that said:

Most of the time you'll probably declare condition variables using the extern or static storage class at file scope, that is, outside of any function. They should have normal (extern) storage class if they are used by other files, or static storage class if they are used only within the file that declares the variable.

Since this static is inside a function, is it auto? or is the class different because of the static key word?

If I moved the 'instance' variable to be static and global (not in any function) in its file - could I still give a reference to it to another file, or would that not work?

Upvotes: 0

Views: 1415

Answers (3)

GingerHead
GingerHead

Reputation: 8230

In general Singleton pattern is used in projects so that there will be only one instance used in that project. like DB connection pool generators or configuration. Singleton is used in the following way:The instance which will always be called must be static and private in the class, the constructor of the class must be private, the getInstance function must be public. Let's see below:

/**
 * @author harzumanian
 *
 */
public class LogManager {

    private File file = null;
    private static LogManager logger = null;

    /**
     * 
     */
    private LogManager() {
        file = new File(Constants.PLUGIN_LOG);
    }

    public static LogManager getLogger() {
        if (logger == null) {
            logger = new LogManager();
        }
        return logger;
    }
}

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279285

I think the author has conflated storage duration and linkage, although the advice what keywords to use is correct.

Your function-scope variable instance has static storage duration. Without the static keyword it would have automatic storage duration.

An object at file scope always has static storage duration (well, or thread storage duration in C++11 with keyword thread_local). Using the static keyword at file scope gives the object internal linkage, extern gives it external linkage, they don't affect storage duration.

Describing something has having "static storage class" is correct, since static is a so-called "storage class specifier", but it's not the whole story since the meaning of static in file scope is different from the meaning in function scope.

You can pass around a reference to an object with static storage duration to code from other translation units. It doesn't matter whether it has file scope or function scope, and it doesn't matter what its linkage is. "They should have normal (extern) storage class if they are used by other files" just means that a global needs external linkage if other files are going to refer to it by name, since a global name with internal linkage refers to a different object in each TU. Technically it's the name that has linkage, not the object.

Upvotes: 4

Alok Save
Alok Save

Reputation: 206546

instance has an Static storage class since it is qualified with the static qualifier without it it would be automatic variable with local scope.
static variables declared at file scope have internal linkage and you cannot use them across different files.
if you want to declare a global variable which can be used across files then you need to use extern which gives the variable external linkage.

Upvotes: 4

Related Questions