Supplement: C++ Code Examples

Example 16-3. C++ Code Fragment: Singleton Pattern
Class USTax {
  public:
    static USTax* getInstance();
  private:
    USTax();
    static USTax* instance;
}
USTax* USTax::instance= 0;

USTax* USTax::getInstance () {
  if (instance== 0) {
     instance= new USTax;
  }
return instance;
}

Example 16-4. C++ Code Fragment: Double-Checked Locking Pattern
class USTax : public CalcTax {
  public:
    static USTax* getInstance();
  private:
    USTax();
    static USTax* instance;
};
USTax* USTax::instance= 0;

USTax* USTax::getInstance () {
  if (instance== 0) {
    // do sync here
    if (instance== 0) {
        instance= new USTax;
    }
  }
  return instance;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.78.30