欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++單例類模板詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

單例類

描述

指在整個(gè)系統(tǒng)生命期中,一個(gè)類最多只能有一個(gè)實(shí)例(instance)存在,使得該實(shí)例的唯一性(實(shí)例是指一個(gè)對象指針)  , 比如:統(tǒng)計(jì)在線人數(shù)

在單例類里,又分為了懶漢式和餓漢式,它們的區(qū)別在于創(chuàng)建實(shí)例的時(shí)間不同:

  1. 懶漢式 : 指代碼運(yùn)行后,實(shí)例并不存在,只有當(dāng)需要時(shí),才去創(chuàng)建實(shí)例(適用于單線程)
  2. 餓漢式 : 指代碼一運(yùn)行,實(shí)例已經(jīng)存在,當(dāng)時(shí)需要時(shí),直接去調(diào)用即可(適用于多線程)

用法

  1. 將構(gòu)造函數(shù)的訪問屬性設(shè)置為private,
  2. 提供一個(gè)GetInstance()靜態(tài)成員函數(shù),只能供用戶訪問唯一一個(gè)實(shí)例.
  3. 定義一個(gè)靜態(tài)成員指針,用來供用戶獲取
  4. 重載 (=)賦值操作符以及拷貝構(gòu)造函數(shù),并設(shè)為private, 避免對象間拷貝,復(fù)制.

初探單例類-懶漢式:

#include <iostream>
using namespace std; 

class CSingleton
{
private:
    static CSingleton* m_pInstance;

    CSingleton()     //構(gòu)造函數(shù)為private
    {
    } 

    CSingleton& operator = (const CSingleton& t);
    CSingleton(const CSingleton &);
public:
    static CSingleton* getInstance()
    {
       if(m_pInstance==NULL)
           m_pInstance= new CSingleton(); 
      
        return m_pInstance;
    }

    void print()
    {
      cout<<this<<endl;   
    }
};

CSingleton* CSingleton::m_pInstance = NULL;

int main()
{
    CSingleton *p1=CSingleton::getInstance();
    CSingleton *p2=CSingleton::getInstance(); 
    CSingleton *p3=CSingleton::getInstance();

    p1->print();
    p2->print();
    p3->print();

    return 0;
}

運(yùn)行打印:

0x6e2d18
0x6e2d18
0x6e2d18

從打印結(jié)果可以看出,該指針對象指向的都是同一個(gè)地址,實(shí)現(xiàn)了一個(gè)類最多只能有一個(gè)實(shí)例(instance)存在.

注意:由于實(shí)例(instance),在系統(tǒng)生命期中,都是存在的,所以只要系統(tǒng)還在運(yùn)行,就不需要delete

上面的懶漢式如果在多線程情況下 ,多個(gè)Csingleton指針對象同時(shí)調(diào)用getInstance()成員函數(shù)時(shí),由于m_pInstance = NULL,就會創(chuàng)建多個(gè)實(shí)例出來.

所以,在多線程情況下,需要使用餓漢實(shí)現(xiàn)

代碼如下:

class CSingleton
{
private:

    static CSingleton* m_pInstance;

    CSingleton()     //構(gòu)造函數(shù)為private
    {
    }       

    CSingleton& operator = (const CSingleton& t);
    CSingleton(const CSingleton &); 

public:
    static CSingleton* getInstance()
    { 
        return m_pInstance;
    }
};

CSingleton* CSingleton::m_pInstance = new CSingleton;

單例類模板

我們現(xiàn)在講解的僅僅是個(gè)框架,里面什么都沒有,不能滿足需求啊,所以還要寫為單例類模板頭文件,當(dāng)需要單例類時(shí),直接聲明單例類模板頭文件即可

寫CSingleton.h

#ifndef _SINGLETON_H_
#define _SINGLETON_H_

template <typename T>
class CSingleton
{
private:
    static T* m_pInstance;

    CSingleton()     //構(gòu)造函數(shù)為private
    {   
     }    
    
public:
    static T* getInstance()
    {  
        return m_pInstance;
    }
};

template <typename T>
T* CSingleton<T> :: m_pInstance = new T;

#endif

當(dāng)我們需要這個(gè)單例類模板時(shí),只需要在自己類里通過friend添加為友元即可,

接下來試驗(yàn)單例類模板

寫main.cpp

#include <iostream>
#include <string>
#include "CSingleton.h"

using namespace std;

class Test
{
    friend class CSingleton<Test> ; //聲明Test的友元為單例類模板
private:  
    string mstr;

    Test(): mstr("abc")
    {
    }

    Test& operator = (const Test& t);
    Test(const Test&);

public:  
    void Setmstr(string t)
    {
     mstr=t;
    }

    void print()
    {
    cout<<"mstr = "<<mstr<<endl;
    cout<<"this = "<<this<<endl;
    }
};

int main()
{
    Test *pt1 = CSingleton<Test>::getInstance();
    Test *pt2 = CSingleton<Test>::getInstance();  

    pt1->print();
    pt2->print();

    pt1->Setmstr("ABCDEFG");
    pt2->print();

    return 0;
}

mstr = abc
this = 0x2d2e30

mstr = abc
this = 0x2d2e30

mstr = ABCDEFG
this = 0x2d2e30

以上所述是小編給大家介紹的C++單例類模板詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!

上一篇:C++多線程實(shí)現(xiàn)電子詞典

欄    目:C語言

下一篇:淺談關(guān)于C語言中#define的副作用

本文標(biāo)題:C++單例類模板詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/402.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有