設計模式之單例模式

2021-08-01 21:17:06 字數 1094 閱讀 8662

此單例模式非記憶體洩漏非執行緒安全。

singleton.h

#pragma once

#define disallow_copy(typename) \

typename(const typename&){}

#define disallow_assign(typename) \

typename operator=(const typename&){}

#define disallow_copy_and_assign(typename) \

typename(const typename&) {} \

typename operator=(const typename&){}

#include

#include

using namespace std;

class singleton

// ~deleter()

//

// }

// };

// // 定義乙個靜態的deleter例項

// static deleter deleter;

};

singleton.cpp

#include "singleton.h"

singleton *singleton::m_pinstance;

//std::shared_ptrsingleton::m_pinstance;

//singleton::deleter singleton::deleter;

singleton::~singleton()

singleton::singleton()

singleton *singleton::getinstance()

// return m_pinstance.get();

}

切記一點:qt專案中,單例模式主動delete窗體會和qt本身物件樹析構機制衝突,造成二次析構,從而導致出現無法訪問0xfffffffe之類的錯誤,qt析構機制只是將物件delete掉,並不會將物件置為null。

設計模式之單例模式

前一段時間買了一本秦小波寫的 設計模式之禪 網上對這書的評價很高。現在還沒有看很多,但是有些地方頗有感觸,也並不是所有的地方都能看懂,但是會慢慢研究的。自己對於設計模式的感覺就是乙個字 牛!感覺會23種設計模式並且會熟練運用的人,真的就是大師級的牛人了,設計模式是乙個專案主管或者架構師一定要會的東西...

設計模式之單例模式

package com.xie.singleton public class singleton 提供乙個共有的靜態的入口方法 public static singleton getinstance 懶漢式 延遲載入 提供乙個私有的靜態的成員變數,但不做初始化 private static sing...

設計模式之 單例模式

單例模式 singleton 保證乙個類僅有乙個例項,並提供乙個訪問它的全域性訪問點。單例模式 單件模式 使用方法返回唯一的例項 public class singleton private static singleton instance public static singleton geti...