static成员必须在类外初始化,常量和变量的区别

xiaoxiao2021-02-28  84

// Array.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <iostream.h> class A{ private: static A* const pa; A(){} public: static A* const GetA(){ return pa; } void hello() { cout<<"Hello!"<<endl; } }; A* const A::pa=new A(); //const static A pa; int main(int argc, char* argv[]) { A* pa=A::GetA(); pa->hello(); }

 // Array.cpp : Defines the entry point for the console application.//

#include "stdafx.h"#include <stdio.h>#include <iostream.h>

class A{private:    static  A*  const pa;       A(){}public:    static A* const GetA(){      return pa;    }    void hello() {          cout<<"Hello!"<<endl;    }};A* const A::pa=new A();//const static  A  pa;

int const  getConstA(){      return 1;}

int  getConstB(){      return 1;}int main(int argc, char* argv[]){ const int a=getConstA(); const int b=getConstB(); int c=getConstA(); int d=getConstB();    A* pa=A::GetA(); pa->hello();}

转载请注明原文地址: https://www.6miu.com/read-74154.html

最新回复(0)