当前位置:编程学习 > C/C++ >>

c++静态那些事

[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
 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,