为什么我的班级成员变量没有更新?

时间:2018-08-28 16:55:44

标签: c++

我是C ++编程的新手,但是我不确定为什么在给它赋值后我的成员变量没有更新。

更新代码:

主要

#include <stdio.h>
#include "PrintingClass.h"

int main()
{

PrintingClass::Instance().printing(); //does not works

  //  PrintingClass a;  //works
  //  a.printing();     //works

    return 0;
}

PrintingClass.h

#include <stdio.h>  
#include <stdint.h>     
class PrintingClass   
{  
public:   
    PrintingClass();  

    void printing();  

    uint16_t variable;  

    // Singleton access  
    static inline PrintingClass & Instance()  
    {
        return *m_instance;  
    }   

    private:  
    static PrintingClass * m_instance;      
};  

P rintingClass.cpp

#include "PrintingClass.h" 
class PrintingClass * PrintingClass::m_instance = 0;

// Constructor
PrintingClass::PrintingClass()
{
    m_instance = this;
}

void PrintingClass::printing()
{
    variable = 0;
    printf("variable %d\r\n",variable);
    variable = 1;
    printf("variable %d\r\n",variable);
    variable = 2;
    printf("variable %d\r\n",variable);
}

输出:

Variable 0
Variable 0
Variable 0

0 个答案:

没有答案