[cpp]
#include <QtCore/QCoreApplication>
#include <iostream>
#include <stdio.h>
using namespace std;
class A
{
public:
A();
static void GetStaticNum(); //靜態函數
void GetNum(); //普通函數
void UseStatic(); //普通函數調用靜態函數
int j; //普通變量成員
static int i; //靜態成員變量
};
int A::i = 0; //靜態成員變量初始化
A::A()
{
j=2;
}
void A::GetStaticNum()
{
i++;
cout<<"wyz=========GetStaticNum() i="<<i<<endl;
}
void A::GetNum()
{
j++;
cout<<"wyz=========GetNum() i="<<i<<endl;
cout<<"wyz=========GetNum() j="<<j<<endl;
}
void A::UseStatic()
{
static int k =0; //靜態變量
k++;
cout<<"wyz=========UseStatic() k="<<k<<endl;
GetStaticNum();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
A aa1;
aa1.GetNum();
A aa2;
aa2.GetNum();
A aaa1;
aaa1.GetStaticNum(); //靜態函數調用方法一
A::GetStaticNum(); //靜態函數調用方法二
A *aa = new A();
aa->GetNum();
aa->UseStatic();
aa->UseStatic();
delete aa;
return 0;
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <iostream>
#include <stdio.h>
using namespace std;
class A
{
public:
A();
static void GetStaticNum(); //靜態函數
void GetNum(); //普通函數
void UseStatic(); //普通函數調用靜態函數
int j; //普通變量成員
static int i; //靜態成員變量
};
int A::i = 0; //靜態成員變量初始化
A::A()
{
j=2;
}
void A::GetStaticNum()
{
i++;
cout<<"wyz=========GetStaticNum() i="<<i<<endl;
}
void A::GetNum()
{
j++;
cout<<"wyz=========GetNum() i="<<i<<endl;
cout<<"wyz=========GetNum() j="<<j<<endl;
}
void A::UseStatic()
{
static int k =0; //靜態變量
k++;
cout<<"wyz=========UseStatic() k="<<k<<endl;
GetStaticNum();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
A aa1;
aa1.GetNum();
A aa2;
aa2.GetNum();
A aaa1;
aaa1.GetStaticNum(); //靜態函數調用方法一
A::GetStaticNum(); //靜態函數調用方法二
A *aa = new A();
aa->GetNum();
aa->UseStatic();
aa->UseStatic();
delete aa;
return 0;
return a.exec();
}
結果:
[cpp]
Starting /root/qtdir/StaticFunc-build-desktop/StaticFunc...
wyz=========GetNum() i=0
wyz=========GetNum() j=3
wyz=========GetNum() i=0
wyz=========GetNum() j=3
wyz=========GetStaticNum() i=1
wyz=========GetStaticNum() i=2
wyz=========GetNum() i=2
wyz=========GetNum() j=3
wyz=========UseStatic() k=1
wyz=========GetStaticNum() i=3
wyz=========UseStatic() k=2
wyz=========GetStaticNum() i=4
/root/qtdir/StaticFunc-build-desktop/StaticFunc exited with code 0
Starting /root/qtdir/StaticFunc-build-desktop/StaticFunc...
wyz=========GetNum() i=0
wyz=========GetNum() j=3
wyz=========GetNum() i=0
wyz=========GetNum() j=3
wyz=========GetStaticNum() i=1
wyz=========GetStaticNum() i=2
wyz=========GetNum() i=2
wyz=========GetNum() j=3
wyz=========UseStatic() k=1
wyz=========GetStaticNum() i=3
wyz=========UseStatic() k=2
wyz=========GetStaticNum() i=4
/root/qtdir/StaticFunc-build-desktop/StaticFunc exited with code 0
作者:wyz365889