본문 바로가기

카테고리 없음

static class의 refernce를 받아오기 까다로울때


#include <iostream>
#include <stack>
#include <queue>
using namespace std;


class A{

 int bbb;

public:
 static A* a;

 static A* getA(){
  if(a == NULL)
   a = new A();
  return a;
 }
 A(){
  cout << "test" << endl;
  bbb = 0;
 }
 void setB(int _b){
  bbb = _b;
 }
 int getB(){
  return bbb;
 }
};
A* A::a = NULL;  // 이렇게 안하면 외부참조오류가 발생합니다.

void main()
{
 A* b = A::getA();
 cout << "test b: " << b->getB() << endl;

 b->setB(555);

 A* c = A::getA();

 cout << "test c: " << c->getB() << endl;

 
 
 

}