Qt静的メンバー関数の呼び出し非静的メンバー変数、非静的メンバー関数

2693 ワード

  • 複数人が開発したプロジェクトでは、いくつかの一般的なツールクラスインタフェースが静的メンバー関数を使用することができる.呼び出しが便利で、エラーの呼び出しが容易ではありません.
  • 静的メンバー関数はthisポインタを渡さず(できない->)、オブジェクト個体を認識しないため、静的データメンバーの操作によく使用されます.非静的メンバー変数は操作できません.
  • 静的メンバー関数非静的メンバー変数にアクセスする必要がある場合は、オブジェクトによって参照する必要があります.
  • #ifndef MYSTAIC_H
    #define MYSTAIC_H
    
    #include 
    #include 
    #include 
    #include 
    
    class MyStaic : public QObject
    {
        Q_OBJECT
    public:
        explicit MyStaic(QObject *parent = 0);
        ~MyStaic();
        static  void  test();
        static  void  test(MyStaic &myStaic);
        static QMutex*  mutex;
        void  notStaticfun();
    private:
        int  tmp ;
    
    };
    
    #endif // MYSTAIC_H
    
  • MyStaicはstaticテストクラスとして,testは通常の静的関数として用いられる.
  • パラメータ付きtest関数は、非静的メンバー変数にアクセスする静的関数
  • として機能する.
  • mutexは静的メンバー変数であり、静的メンバー関数で
  • を呼び出すために使用される.
  • notStaticfun()関数は通常のメンバー関数であり、静的メンバー関数呼び出し
  • に使用される.
    #include "mystaic.h"
    QMutex*  MyStaic::mutex = new QMutex;
    
    MyStaic::MyStaic(QObject *parent) : QObject(parent)
    {
        qDebug() << "This is      ";
        tmp = 10;
    }
    
    
    MyStaic::~MyStaic()
    {
        qDebug() << "this is   ";
    }
    
    void MyStaic::test()
    {
        qDebug() <<  "OK";
        mutex->lock();
        mutex->unlock();
    
    
    }
    
    void MyStaic::test(MyStaic &myStaic)
    {
        qDebug() << "This is not static Member variables tmp =" << myStaic.tmp ;
        myStaic.notStaticfun();
    }
    
    void MyStaic::notStaticfun()
    {
        qDebug() <<  "This  is  not  static  fun";
    }
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "mystaic.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        MyStaic::test();
        MyStaic*  myNoStatic = new MyStaic(this);
        MyStaic::test(*myNoStatic);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    #include "mainwindow.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    このプログラムの実行結果は次のとおりです.
    OK
    This isコンストラクタ
    This is not static Member variables tmp = 10
    This is not static fun
    this is析出