Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does static mean when applied to a class at namespace scope?

I know that static at namespace scope means "internal linkage". Now consider the following code:

static class Foo {} foo;

Does the static apply to Foo, foo or both?

like image 967
fredoverflow Avatar asked Aug 04 '11 14:08

fredoverflow


People also ask

What is static namespace?

static variable at namespace scope (global or otherwise) has internal linkage. That means, it cannot be accessed from other translation units. It is internal to the translation unit in which it is declared.

Are variables in namespace static?

When you declare a variable as static , it means that its scope is limited to the given translation unit only. Without static the scope is global. When you declare a variable as static inside a . h file (within or without namespace ; doesn't matter), and include that header file in various .

What is static member of a class in C++?

When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

What is scope of static data member?

The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile . You cannot declare a static data member as mutable . You can only have one definition of a static member in a program.


1 Answers

It applies to the variable declared after the class definition.

In C++, there is no such thing as static class. There are only static objects and static functions.

like image 99
Nawaz Avatar answered Nov 03 '22 01:11

Nawaz