Jump to content
  • Advertisement
Sign in to follow this  
Cibressus

static and const

This topic is 5477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.cpp
namespace
{
int foo;
}
//implicit "using namespace" so you can access foo normally
foo = 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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
...
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!