static variables

E

Eric Kaplan

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


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

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
 
D

DDD

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

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

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.
A static member is shared among all objects of that class.A static
member can be invoked directly from the class using the scope operator
or indirectly through an object, reference, or pointer to an object of
its class type.
2) static variable can be access from any where through the Class name

A static data member is declared in the class declaration and is
initialized in the file containing the class methods. The scope
operator
is used in the initialization to indicate to which class the static
member
belongs. However, if the static member is a const integral type or an
enumeration type, it can be initialized in the class declaration
itself.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top