static and const

Started by
9 comments, last by Qw3r7yU10p! 19 years, 7 months ago
lemme just double check something. the keywords static: means that the varible stays in memory after the fucntion has terminated const: means the varible does not change right? also, what is ment by the word cast when talking about programing?
| Member of UBAAG (Unban aftermath Association of Gamedev)
Advertisement
It is also possible to have a static global variable. If you have multiple CPP files, then it will only be accessible in the file it is declared in (extern won't work on it). This is kind of a poor choice of keywords, it seems to mean basically the opposite of the usual usage...

A variable declared as const cannot be changed. This gets a little stickier with pointers:
const char * x; // What x points to cannot be modified
char * const x; // x can not be modified, but *x can
const char * const x; // x and *x cannot be modified
You can also have const member functions. These are not allowed to modify the object (except for members declared mutable) or call other non-const member functions.

Casting refers to converting an object from one type to another.
Quote:Original post by uavfun
It is also possible to have a static global variable. If you have multiple CPP files, then it will only be accessible in the file it is declared in (extern won't work on it). This is kind of a poor choice of keywords, it seems to mean basically the opposite of the usual usage...

A variable declared as const cannot be changed. This gets a little stickier with pointers:
const char * x; // What x points to cannot be modified
char * const x; // x can not be modified, but *x can
const char * const x; // x and *x cannot be modified
You can also have const member functions. These are not allowed to modify the object (except for members declared mutable) or call other non-const member functions.

Casting refers to converting an object from one type to another.


The use of static to create "local global" variables is depreciated. You're supposed to use anonymous namespaces now.

//file1.cppnamespace{  int foo;}//implicit "using namespace" so you can access foo normallyfoo = 5;


Also, I think static means that the variable once initialized exists for the entire duration of a program until it terminates. Hope that clears a few things up.
For globals static means not visible ouotside the compiled module.
inside functions static means global variable that is only accessable by the function it is defined in.

for example

void Edges (vec *tri) {
static int next[3] = {1, 2, 0};

for (int i = 0;i < 3;i++) {
int p1 = i;
int p2 = next;
...
}
}
There are other less frequently used storage type modifiers like 'volatile' and 'register'.
volatile forces the compiler to load the variable from memory every time. Could be used when another thread is writing to the same variable for example.
register suggests to the compiler that the variable should be kept in a register. It has little or no effect on modern compilers though which usually figure out what's best for themselves.

You can also declare functions as static which means they aren't accessabile from other files. This allows extra compiler optimisations too.

A cast is the operation of telling the compiler you wish to interpret some data as if it were of a different type.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Warning: OT
Quote:Original post by iMalc
There are other less frequently used storage type modifiers like 'volatile' and 'register'.
volatile forces the compiler to load the variable from memory every time. Could be used when another thread is writing to the same variable for example.
register suggests to the compiler that the variable should be kept in a register. It has little or no effect on modern compilers though which usually figure out what's best for themselves.
And since we're listing the storage modifiers..
auto: The default, i.e. neighter static, volatile nor register.
extern: Declares a function or data without defining it. The default for function declarations although necessary for global data.

Also be careful when using volatile for thread synchronization. There's no guarantee that a read or write is atomical.
Quote:Original post by iMalc
You can also declare functions as static which means they aren't accessabile from other files. This allows extra compiler optimisations too.
Static also has a different meaning in the context of member functions.
Quote:Original post by iMalc
You can also declare functions as static which means they aren't accessabile from other files. This allows extra compiler optimisations too.


Thats valid in C but this is a deprecated feature in c++ to achieve the same thing is by using anonymous namespaces.
const global variables that have not been declared with extern also have internal linkage, just like static global variables.
also, IIRC
const char * c;
and
char const * c;
is the same, as well as
int const
and
const int

I'm wondering why - there must be not more than one legal way to define something....
Quote:Original post by Dmytry
also, IIRC
const char * c;
and
char const * c;
is the same, as well as
int const
and
const int

I'm wondering why - there must be not more than one legal way to define something....
Ah, but there is...
http://www.research.att.com/~bs/bs_faq2.html#const
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement