 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tales Aguiar Guest
|
Posted: Wed Jun 08, 2005 1:52 pm Post subject: static const and functions |
|
|
What is the difference betwenn simple const and static const?
What is the difference betwenn simple function and static function?
like:
const int a = 1;
static const int b = 1;
void c()
{
}
static void d()
{
}
Tales Aguiar.
|
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Wed Jun 08, 2005 2:37 pm Post subject: Re: static const and functions |
|
|
"Tales Aguiar" <tqueiroz (AT) engesis (DOT) com> wrote:
| Quote: | What is the difference betwenn simple const and static const?
What is the difference betwenn simple function and static function?
like:
const int a = 1;
static const int b = 1;
void c()
{
}
static void d()
{
}
|
Are you sure this isn't a homework question?
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
|
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Wed Jun 08, 2005 2:38 pm Post subject: Re: static const and functions |
|
|
The story of 'const' is simple but 'static' is used in a couple of different
ways so its tale is more involved.
When an item is declared in a file outside of any function, class or
structure its name is added to the list of public symbols. Items on the
list of public symbols are visible to all files in the project and are said
to be 'global'.
If the item is decorated with the keyword 'static' then the name is not
added to the list of public symbols. It is known from then on in the file
but is not known to items outside of that file and is said to be 'static'.
Several years ago a change to the C++ language specification altered the
behavior of a 'const' . A 'const' is now 'static' by default. It is not
added to the list of public symbols so is not known to items outside of the
current file. Therefore the use of 'static const' is redundant. It already
defaults to being 'static'.
Functions that are declared as 'static' are known only in the source file in
which they reside. Data items which are declared 'const' but without the
keyword 'static' can be made known by others.
To make a 'const' data item known globally one could do it in either of two
ways:
1-
Put the declaration of the item in a header file.
const int a = 1; // in a header file
2 -
Declare the item in the normal fashion, for example:
const int a = 1;
Provide an 'extern' declaration in a header file that is
used both by the source file containing the declaraiton
and by all source files that will use it:
extern const int a;
Method 1 is useful for simple integral values such as int, unsigned, bool
and char.
Method 2 is more useful for items which are not simple integral values such
as an array
In the source file:
const int values[] = { 12, 13, 14, 15 };
In the header file included by the source file and
any other source file which uses the item:
extern const int values[];
The use of the keyword 'static' inside of a function, a structure or a class
works differently than if the item is declared outside of these items.
A variable declared inside of a function with the keyword 'static' means it
is not an 'automatic' variable so is not allocated when the function begins
and released when the function ends. Instead it is allocated throughout the
life of the program and retains its value across multiple function calls.
A variable or function with the keyword 'static' in a class or structure
does not have a copy for each instance of the class or structure. There is
only one of them shared by all instances. For a variable there is a need to
also declare the variable in the file outside of the class definition. For
a function this means the code has no knowledge of which instance of the
class or structure is the current one in whose context it was called so it
cannot access normal, non-static, class member variables unless a 'this'
pointer is provided and used.
-----------------------
class Example
{
private:
int var;
static int stat_var;
public:
Example() : var(0) {}
~Example() {}
int Value() const { return var; }
void Value(int new_val) { var = new_val; }
static int StaticValue() { return static_var; }
static int StaticValue(int new_val) { static_var = new_val; }
}; // end class Example
int Example::stat_var; // storage declaration of static variable
-----------------------
.. Ed
"Tales Aguiar" <tqueiroz (AT) engesis (DOT) com> wrote
| Quote: | What is the difference betwenn simple const and static const?
What is the difference betwenn simple function and static function?
like:
const int a = 1;
static const int b = 1;
void c()
{
}
static void d()
{
}
Tales Aguiar.
|
|
|
| Back to top |
|
 |
Tales Aguiar Guest
|
Posted: Wed Jun 08, 2005 8:41 pm Post subject: Re: static const and functions |
|
|
Thanks!!!
You explained my last doubts.
E2451 no more!
:)
Tales Aguiar
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|