Cpp Static Riddle

Name the 5 possible meanings of "static" in C++.

Answer (From Steve Oualline's Practical C++ Programming ISBN 1-56592-139-9 ) Context: Meaning

Variable outside all function bodies: Variable scope is limited to file in which declared

...unless the declaration is enclosed in an anonymous NameSpace

 namespace 
 {
	int nBetterThanStaticInt = SOME_VALUE;
 }

Function declaration: Function scope is limited to file in which declared

Same NameSpace quibble as above. In fact, I don't see the difference between this case and the previous (except that the name declared is the name of a function rather than a data object).

In any case the word scope is being abused here. The scope of a declaration outside a function body (or NameSpace) is always from the point of declaration to the end of the containing translation unit. That is as far as a "scope" can extend... other translation units are separately compiled (that's what makes them separate "translation units"), so the scope of this translation unit cannot extend into them. The concept that declarations of the same name with the same type in different translation units can be taken to refer to the same underlying entity at link time is called linkage, not scope. In declarations "at file scope", the keyword "static" specifies "internal linkage".

Variable inside a function: Variable is permanent; initialized precisely once.

i.e., the variable has "static storage class"

This is reminiscient of a global variable whose scope is limited to the function in which it is defined. Hilarity ensues.
  • Since "global" means its scope is program-wide, you are contradicting yourself.

Member variable: One copy of variable created per class [== Smalltalk "class variable"]

	 : If I have 100 instances of a particular class, I can change any normal variable in any instance and the other 99 stay the same.
	 : But if I change any static variable through any instance, the other 99 instances will refer to the new value of "the" static variable. There is no "copy" of this one variable. (EditHint: make more succinct.)

No! No! No! This is a pet-peeve of mine, a static member is absolutely nothing like a SmallTalk class variable. Check out what a metaclass is.
This is in effect a global variable which is scoped to the class in which it is defined. If in doubt, examine the fact that the variable must be declared in the class declaration as well as defined with class scope in a source file. Better?
  • Again with the self-contradiction. I think you must mean a word other than "global".
Member function: Function can only access static members of class. [== Smalltalk "class method"]
Not strictly true as stated. It is true that a static member function of class X does not have its own "this" pointer, but if it has access to a pointer-to-X or reference-to-X by other means (parameter passing, global variable, etc.) it does have permission to dereference the pointer or reference in order to obtain access to the referenced object's instance members, even if they are private.

Once again, a static method is nothing like a class method. A class member is an actual instance member of the single instance of the class class. A static member is just some function given the NameSpace of the class. It can not be overloaded, it has no this, etc. Completely different, apples and oranges. --rad (But... a C++ static method does have much in common with a Smalltalk class method. The fact that there are major differences does not take that away. A static method is the closest analogy in C++ to a Smalltalk class method, unless you choose to implement layers on top of the basic C++ object model. --JamesDennett)

''rad, I assume you meant overridden not overloaded above. static methods can be overloaded, they just can't be virtual.


I've got a question on this topic. Is a static variable defined in a class method, similar to a member variable that has scope only for that method? In other words is a new static variable created for each instance of the class?

No, see above for the meaning of static method variable and static instance variable.

 class A
 {
  int bananas;
  static int total_bananas;
  int f();
  static int z();
 };

int A::f() { static int x = 0; return ++x; }

void g() { A a1; A a2;

int x1 = a1.f(); int x2 = a2.f(); int x3 = a2.f(); // what are the values of x1, x2 and x3 here? }

According to g++, 1, 2, and 3. - SebastianBozlee


In other words, everything at the compilation unit scope in the symbol table, and it is only externally visible if it is part of a class or NameSpace.


The use of static to indicate "local to translation unit" is deprecated in C++. Use unnamed NameSpaces instead. [Bjarne Stroustrup, The C++ Programming Language, 3rd ed, pg 819]

In C and older C++ programs, the keyword static is (confusingly) used to mean "use internal linkage" Don't use static except inside functions and classes. [Bjarne Stroustrup, The C++ Programming Language, 3rd ed, pg 200]

OliverKamps

static_cast: a limited form of casting that respects C++ typing rules.
CategoryCpp

EditText of this page (last edited June 27, 2009) or FindPage with title or text search