static variable

J

June Lee

How do I define a static (Class variable) just like in Java?

from the following class it has a static variable
http://www.oniva.com/upload/1356/TestAppWLib.cpp

static BYTE msgDUMP[5] = { 0xf0, 0x7d, 0x00, 0x47, 0xf7};
static BYTE msgMUTE[6] = { 0xf0, 0x7d, 0x00, 0x32, 0x01, 0xf7};
static BYTE msgUNMUTE[6] = { 0xf0, 0x7d, 0x00, 0x32, 0x00, 0xf7};

right before the constructor

CTestAppWLibDlg::CTestAppWLibDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestAppWLibDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

====================
is that how people define static (Class variable) in C++?

does people define static variable in .h header file?

what's difference with no static keyword, would that become a global
variable?

I think difference of global var and static variable are:

1) global variable can be access from only the own .cpp file.

2) static variable can be access from any where through the Class name
 
J

James Kanze

How do I define a static (Class variable) just like in Java?

More or less like you would in Java. With the difference that
in C++, static and function members of a class are only declared
in the class definition, not defined, so you need a definition
elsewhere.

Technically, there are differences with regards to Java---there
is no object for the class in C++, and the term "class
variable", as opposed to "instance variable" is not used, since
most people would understand "class variable" to be opposed to
"local variable" or "namespace variable", i.e. synonym for
"member variable". There's also a difference in lifetime, since
classes aren't "loaded" in C++, but are there from the start.

And of course, because C++ supports variables at namespace
scope, static member variables are a lot less used.
from the following class it has a static
variablehttp://www.oniva.com/upload/1356/TestAppWLib.cpp
static BYTE msgDUMP[5] = { 0xf0, 0x7d, 0x00, 0x47, 0xf7};
static BYTE msgMUTE[6] = { 0xf0, 0x7d, 0x00, 0x32, 0x01, 0xf7};
static BYTE msgUNMUTE[6] = { 0xf0, 0x7d, 0x00, 0x32, 0x00, 0xf7};
right before the constructor
CTestAppWLibDlg::CTestAppWLibDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestAppWLibDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
====================
is that how people define static (Class variable) in C++?

That's how people define static variables at namespace scope in
C++. The variables above, however, are not class members, but
are at namespace scope. (The current preferred practice would
be to declare them in an anonymous namespace, and not use the
keyword static here, but there is still a lot of code written
before this became an option.)

If the variables are part of the implementation (i.e. private),
and all of the implementation of the class is in a single file,
using variables at namespace scope, defined in the file, either
static or in an anonymous namespace, if often preferable to
using a static member variable. It reduces compile time
coupling, since it doesn't require the declaration in the
header. If the variables are part of the interface (protected
or private), however, you'll have to declare them members, so
that client code can see them. In that case, don't forget: the
declaration in the class definition is NOT a definition; you
need a separate definition in one (and only one) of the
implementation files. Something like:

in the header...

class Toto
{
// ...
static double titi ;
// ...
} ;

and in a single source file:

double Toto::titi = 3.14159 ;

(Note that the static keyword is only present in the declaration
in the class, and that the class qualifier Toto:: is necessary
in the definition.)
do people define static variable in .h header file?

There are a (very) few special cases where one might define a
static variable at namespace scope in a header---one classical
case is for inserting version control strings in the object
file, for example (so that you can tell by looking at the object
file which versions of each header were used). Most of the
time, though, variables with static lifetime will be defined in
source files (although they may be declared in a header, so that
other sources can use them).
what's difference with no static keyword, would that become a
global variable?

The static keyword is a bit tricky. In the example you site,
where it is used in the definition of a variable at namespace
scope, it means that the name has internal linkage, rather than
external; i.e. that it can be referred to by other names in
different scopes of the same translation unit, but not from
other translation units. When it is used for a variable a class
definition, it means that the variable is not part of the class
instance, but in fact, has static lifetime, and exists
independantly of any instances of the class. (Basically, you
might say that it is a global variable, but in class scope.)
I think difference of global var and static variable are:
1) global variable can be access from only the own .cpp file.
2) static variable can be access from any where through the
Class name

I think you're mixing up several different concepts. Object
lifetime, scope and linkage are more or less orthogonal. All
declared objects have either static or automatic lifetime. (You
can also explicitly create objects with dynamic lifetime, using
the new operator, and the compiler often creates
objects---temporaries or exceptions, for example---with their
own special lifetimes.) Linkage specifies whether a name
specifies the same thing (object, reference, function, type...)
as a name introduced by a declaration in another scope: a name
can have either external, internal or no linkage. And of
course, a declaration may be a definition, or it might not be.
In general, there may be only one definition of an object or a
function in the entire program, but there must be a declaration
in scope anywhere the object or function is to be used (but
again, there are exceptions).

(The concepts aren't totally orthogonal, of course. It's
impossible for a varible with automatic lifetime, or for that
matter, any variable defined, and not just declared, in local
scope to have linkage.)

Conceptually, C++ is a lot cleaner than Java in this regard; it
keeps separate concepts separate, and applies them orthogonally
to practically everything: types, variables, functions, etc.
Practically, historical considerations mean that there is
absolutely no orthogonality in the declarations, and in the case
of static (by far the worst offender), the keyword sometimes
affects linkage, sometimes lifetime. The important thing is to
first understand the distinct concepts, and then figure out how
the various modifiers in a declaration affect them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top