static void myInit()
{
PrimitiveType=GL_POLYGON;
LineSegments=3;
}
can someone tell me what static does to the function?
static void myInit()
{
PrimitiveType=GL_POLYGON;
LineSegments=3;
}
can someone tell me what static does to the function?
Short answer: the effect of the keyword static depends entirely on where it appears. Inside a class it makes a member “per-class” (not per-object); at namespace/file scope it controls linkage (visibility to other translation units); inside a function it gives the variable static storage duration so it persists across calls. The replies from , and cover parts of this — below are concise clarifications, small examples, and the practical pitfalls alluded to.
In a class a static data member or function belongs to the class itself. A static member function has no implicit this pointer, so it can only operate on static data or on an explicit instance you pass in. Example (C++17 allows in-class initialization for inline statics):
struct S {
inline static int count = 0; // single class-wide value (C++17)
static void inc() { ++count; } // no 'this' pointer here
}; At namespace / file scope, static gives the name internal linkage: the symbol is visible only in that translation unit (what described). In modern C++ prefer an unnamed (anonymous) namespace for TU-local symbols, and avoid putting static data definitions in headers (that yields a separate copy per translation unit). For a single shared definition use extern/out-of-line definition or C++17 inline variables. (cppreference.patrickfasano.com)
A static variable inside a function is constructed once and survives until program exit — useful for counters or the “Meyers singleton”:
int nextId() {
static int id = 0; // initialized once, retained between calls
return ++id;
}
MyType& instance() {
static MyType obj; // construct-on-first-use singleton
return obj;
} Function-local statics are initialized the first time control reaches the declaration and (since C++11) that initialization is guaranteed to occur exactly once even with multiple threads; however non-local static initialization across translation units is unordered (the “static initialization order fiasco”), so avoid inter-unit dependencies or use construct-on-first-use / std::call_once. (cppreference.patrickfasano.com)
Quick troubleshooting summary: if your static void myInit() is at top level it will be visible only in that .cpp; if you want a class-level helper make it a static member, and if you rely on function-local statics be aware of thread-safety (pre-C++11) and hidden-state/testability issues. Use explicit patterns (extern/inline, construct-on-first-use, std::call_once) to avoid surprises.
Jump to Post— JC7 1Member functions (and data members) of a class can be declared as static. This means that they do not belong to any particular instance of the class, but instead are similar to gloabal functions (or variables), but must be referenced using the scope of the class they belong to: Class::staticmemberfunction()
…
Jump to Post— Dave Sinkula 2,398>can someone tell me what static does to the function?
http://www.embedded.com/98/9811/9811fe3.htm about halfway down, although the whole article is worthwhile.
Member functions (and data members) of a class can be declared as static. This means that they do not belong to any particular instance of the class, but instead are similar to gloabal functions (or variables), but must be referenced using the scope of the class they belong to: Class::staticmemberfunction()
Static member functions cannot act on instance data members of a class.
The following link provides an online C++ tutorial, although it is a bit brief:
http://www.cplusplus.com/doc/tutorial/
There are many other free online resources for learning the basics of object oriented programming in C++ that can be found through a google search.
>can someone tell me what static does to the function?
http://www.embedded.com/98/9811/9811fe3.htm about halfway down, although the whole article is worthwhile.
when not in a class definition, 'static' means that the name should not be visible outside the compiled module. This is an older style and has been 'deprecated' by the standards committee, but people still use it all the time.
Here's an example:
// file A
void ExportedRoutine()
{
}
static void NonExportedRoutine()
{
} // File B
// this asks the linker to find ExportedRoutine defined in some other file
extern void ExportedRoutine();
// this won't work because the routine is 'static' and therefore not available
// to the linker to be found...
extern void NonExportedRoutine(); // will generate a linker error Same applies for any declared variable:
static int foo;
int bar; // this could be found by the linker from another obj file.
when not in a class definition, 'static' means that the name should not be visible outside the compiled module. This is an older style and has been 'deprecated' by the standards committee, but people still use it all the time.
Here's an example:
// file A void ExportedRoutine() { } static void NonExportedRoutine() { }// File B // this asks the linker to find ExportedRoutine defined in some other file extern void ExportedRoutine(); // this won't work because the routine is 'static' and therefore not available // to the linker to be found... extern void NonExportedRoutine(); // will generate a linker errorSame applies for any declared variable:
static int foo;
int bar; // this could be found by the linker from another obj file.
Thats good information for me to know thanx, i was familiar with the use of static with regards to classes.
if a variable is declared as static within a function, the variable is maintained and is available for future calls of the function in which it has been declared
>the variable is maintained and is available for future calls of the function in which it has been declared
However, this isn't as useful as it appears at first.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.