c++, singleton

0.

1. Implement a singleton class

class MySingleton{

public:

  static MySingleton*  const p_single;

private:

  MySingleton(void){cout << "private constructor." << endl;}  // private constructor

  MySingleton(const MySingleton&){}  //private copy-constructor

  MySingleton& operator=(const MySingleton&){}  // prevent assignment

};

MySingleton* const MySingleton::p_single = new MySingleton();  // private constructor will be called once

int main(){

  return 0;

}

the output will be: ( order matters)

private constructor.  

posted @ 2020-01-28 03:05  心怀阳光  阅读(87)  评论(0编辑  收藏  举报