Re: 'static' keyword in c vs c++?
"vib.cpp@gmail.com" <vib.cpp@gmail.com> wrote:
I begin to learn C++ when I do not know much about c, just as a lot of
people say that I needn't to learn c first and then stepped to learn c+
+.
But when I was learning the keyword 'static' in c++, it was said that
'static member' is belonging to a class not to an object, so it seems
that static member is a concept from the object-based concepts, but as
I know there is also a 'static' keyword even in c, so what's the
difference about the word in c and c++? why is it also necessary in c
when there is no class ,no objects?
The static keyword had two uses in C and those uses are also supported in
C++. The use of static in objects is a third use of the keyword which
closely parallels the use of the keyword in C.
When used on an external global deceleration, it limits access of the
variable to the file in which it is defined. It prevents it from being
accessed by other code complied separately and then linked.
So when you write:
int abc;
in multiple c source files, and then link the programs together, they are
all accessing the same abc variable;
When you declare it static:
static int abc;
The variable is not linked globally and acts as a local variable in each
file it's used.
You use static externals to limit the scope of variables. It's used in C
to create data hiding - to create global variables that are shared by a set
of common functions, but not accessible to other code.
The second use of static in C is when you use it on a local automatic
variable inside a function:
main()
{
static int abc;
}
This changes the variable abc from being local (on the stack), to being a
global variable that's defined once.
It's basically the same as writing this instead:
static int abc;
main()
{
}
Except the scope of abc is limited to the function or block you declare it
in.
It's used when it's important that the value of the variable is not lost
across function calls. Like this:
int f()
{
static int cnt = 0; // count how many times the function is called
cnt++;
return cnt;
}
That would not work if cnt was not static. It would return 1 every time if
the cnt was a normal automatic variable instead of counting the number of
times the function was called;
C++ simply extends the use of static to members of structures and classes.
C did not support the use of static members inside a structure because it
made no sense to do so since C did not allow functions to be defined inside
of structures.
The meaning of static in structures is as you would suspect. The static
member acts as if it were defined globally (variables are created only once
when the program starts, and remain around until the program ends), but
it's default scope of access is the same as scope for other member
functions (depending on how they are defined).
So even though C did not support static members of structures, the concept
was supported as static variables inside functions so C++ just extended the
same concept structures as part of adding objects.
To say there are no objects in C is totally wrong. People have been
writing object oriented code in C long before C++ was ever invented. In C,
you define objects with structures (just like you do with C++), and you use
them much like you use them in C++. There is no new or delete in C, but
you create the same effect simply by writing constructor and destructor
functions for your objects. I would use names like mb_new() and mb_free()
as the constructor and destructor for the mb (memory buffer) object for
example.
However, because writing in a pure object oriented style in straight C can
make the code somewhat obscure and hard to read, it's often not done to the
extent it could be, so straight C code tends to turn out half structured
style and half OO style. The OO style is used when it must be for dealing
with complex dynamically created data structures. What's seldom done in
straight C because it makes the code far too obscure, is polymorphism and
inheritance.
There's nothing you can do in C++ that you can't do in straight C. The
difference is that C++ takes care a lot of the overhead of creating and
using objects for you making your code far cleaner and simpler and easier
to understand, as well allowing the C++ compiler to do a lot of addition
compile time checks for you that could not be done if you wrote the same
style code in straight C.
--
Curt Welch http://CurtWelch.Com/
curt@kcwc.com http://NewsReader.Com/