欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++使用單例模式實(shí)現(xiàn)命名空間函數(shù)案例詳解

 更新時(shí)間:2023年04月24日 09:09:20   作者:摩天侖  
這篇文章主要介紹了c++使用單例模式實(shí)現(xiàn)命名空間函數(shù),本案例實(shí)現(xiàn)一個(gè)test命名空間,此命名空間內(nèi)有兩個(gè)函數(shù),分別為getName()和getNameSpace(),本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下

本案例實(shí)現(xiàn)一個(gè)test命名空間,此命名空間內(nèi)有兩個(gè)函數(shù),分別為getName()和getNameSpace();

  • 聲明命名空間及函數(shù)
namespace test{
    const std::string& getName()和();
    const std::string& getNameSpace();
}
  • 命名空間內(nèi)實(shí)現(xiàn)單例類
    實(shí)現(xiàn)一個(gè)單例類,構(gòu)造函數(shù)要為private,自身對(duì)象為private
    靜態(tài)成員函數(shù)(才可以調(diào)用靜態(tài)成員變量)
namespace test{
    // 實(shí)現(xiàn)一個(gè)單例類,構(gòu)造函數(shù)要為private,自身對(duì)象為private
    class ThisNode{
    private:
        std::string name_;
        std::string namespace_;
        static ThisNode *thisNode;
        ThisNode():name_("empty"),namespace_("namespace"){};

    public:
        // 靜態(tài)成員函數(shù)(才可以調(diào)用靜態(tài)成員變量)
        /**
         * 函數(shù):實(shí)例化類
         * 返回值:ThisNode&
        */
        static ThisNode& instance(){
            if(thisNode==nullptr){
                std::cout << "沒(méi)有" <<std::endl;
                thisNode = new ThisNode();
                return *thisNode;
            }else{
                std::cout << "有" <<std::endl;
                return *thisNode;
            }
        }
        // 普通成員函數(shù)
        const std::string& getName() const{
            std::cout <<"get name:"<<name_<<std::endl;
            return name_;
        }
        const std::string& getNameSpace() const{
            std::cout <<"getNameSpace:" << namespace_ << std::endl;
            return namespace_;
        }
    };
    // 初始化靜態(tài)成員
    ThisNode *ThisNode::thisNode = nullptr;

    // 實(shí)現(xiàn)命名空間內(nèi)的函數(shù),實(shí)例化一個(gè)類,并調(diào)用函數(shù)
    const std::string& getNameSpace(){
        return ThisNode::instance().getNameSpace();
    }
    const std::string& getName(){
        return ThisNode::instance().getName();
    }

};
  • 實(shí)現(xiàn)命名空間函數(shù)
    首先調(diào)用的是類的靜態(tài)成員函數(shù)實(shí)例化唯一對(duì)象,然后調(diào)用對(duì)象中的方法;
// 實(shí)現(xiàn)命名空間內(nèi)的函數(shù),實(shí)例化一個(gè)類,并調(diào)用函數(shù)
const std::string& getNameSpace(){
	return ThisNode::instance().getNameSpace();
}
const std::string& getName(){
	return ThisNode::instance().getName();
}
  • 調(diào)用
int main(){
    // 使用
    test::getNameSpace();
    test::getName();
    return 0;
}

全部代碼

#include<string>
#include<iostream>

// 聲明命名空間內(nèi)的兩個(gè)函數(shù)
namespace test{
    const std::string& getName()和();
    const std::string& getNameSpace();
}

namespace test{
    // 實(shí)現(xiàn)一個(gè)單例類,構(gòu)造函數(shù)要為private,自身對(duì)象為private
    class ThisNode{
    private:
        std::string name_;
        std::string namespace_;
        static ThisNode *thisNode;
        ThisNode():name_("empty"),namespace_("namespace"){};

    public:
        // 靜態(tài)成員函數(shù)(才可以調(diào)用靜態(tài)成員變量)
        /**
         * 函數(shù):實(shí)例化類
         * 返回值:ThisNode&
        */
        static ThisNode& instance(){
            if(thisNode==nullptr){
                std::cout << "沒(méi)有" <<std::endl;
                thisNode = new ThisNode();
                return *thisNode;
            }else{
                std::cout << "有" <<std::endl;
                return *thisNode;
            }
        }
        // 普通成員函數(shù)
        const std::string& getName() const{
            std::cout <<"get name:"<<name_<<std::endl;
            return name_;
        }
        const std::string& getNameSpace() const{
            std::cout <<"getNameSpace:" << namespace_ << std::endl;
            return namespace_;
        }
    };
    // 初始化靜態(tài)成員
    ThisNode *ThisNode::thisNode = nullptr;

    // 實(shí)現(xiàn)命名空間內(nèi)的函數(shù),實(shí)例化一個(gè)類,并調(diào)用函數(shù)
    const std::string& getNameSpace(){
        return ThisNode::instance().getNameSpace();
    }
    const std::string& getName(){
        return ThisNode::instance().getName();
    }

};

int main(){
    // 使用
    test::getNameSpace();
    test::getName();
    return 0;
}

到此這篇關(guān)于c++使用單例模式實(shí)現(xiàn)命名空間函數(shù)的文章就介紹到這了,更多相關(guān)c++命名空間函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++中結(jié)構(gòu)體的類型定義和初始化以及變量引用

    C++中結(jié)構(gòu)體的類型定義和初始化以及變量引用

    這篇文章主要介紹了C++中結(jié)構(gòu)體的類型定義和初始化以及變量引用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C++17中的折疊表達(dá)式實(shí)現(xiàn)

    C++17中的折疊表達(dá)式實(shí)現(xiàn)

    這篇文章主要介紹了C++17中的折疊表達(dá)式實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • C語(yǔ)言數(shù)組棧實(shí)現(xiàn)模板

    C語(yǔ)言數(shù)組棧實(shí)現(xiàn)模板

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)組棧實(shí)現(xiàn)模板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 基于QT的TCP通信服務(wù)的實(shí)現(xiàn)

    基于QT的TCP通信服務(wù)的實(shí)現(xiàn)

    在項(xiàng)目開(kāi)發(fā)過(guò)程中,很多地方都會(huì)用到TCP通信,本文主要介紹了基于QT的TCP通信服務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 利用Matlab繪制地圖的超詳細(xì)教程

    利用Matlab繪制地圖的超詳細(xì)教程

    worldmap和usamap是axesm的子類,worldmap是用于生成世界地圖坐標(biāo)區(qū)域,usamap用于生成美國(guó)地圖坐標(biāo)區(qū)域。本文將詳細(xì)為大家介紹如何利用這兩個(gè)函數(shù)繪制地圖,需要的可以參考一下
    2022-02-02
  • C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)通關(guān)時(shí)間復(fù)雜度和空間復(fù)雜度

    C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)通關(guān)時(shí)間復(fù)雜度和空間復(fù)雜度

    對(duì)于一個(gè)算法,其時(shí)間復(fù)雜度和空間復(fù)雜度往往是相互影響的,當(dāng)追求一個(gè)較好的時(shí)間復(fù)雜度時(shí),可能會(huì)使空間復(fù)雜度的性能變差,即可能導(dǎo)致占用較多的存儲(chǔ)空間,這篇文章主要給大家介紹了關(guān)于C語(yǔ)言時(shí)間復(fù)雜度、空間復(fù)雜度的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 清除3389遠(yuǎn)程登錄日志

    清除3389遠(yuǎn)程登錄日志

    這篇文章主要介紹了清除3389遠(yuǎn)程登錄日志示例,需要的朋友可以參考下
    2014-01-01
  • C++算法學(xué)習(xí)之貪心算法的應(yīng)用

    C++算法學(xué)習(xí)之貪心算法的應(yīng)用

    貪心算法是指,在對(duì)問(wèn)題求解時(shí),總是做出在當(dāng)前看來(lái)是最好的選擇。本文為大家準(zhǔn)備了幾個(gè)示例,從而能深入了解貪心算法的應(yīng)用,需要的可以參考一下
    2022-05-05
  • 在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法

    在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法

    經(jīng)常會(huì)遇到類型轉(zhuǎn)換,本文主要介紹了C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C++動(dòng)態(tài)內(nèi)存管理詳解

    C++動(dòng)態(tài)內(nèi)存管理詳解

    今天小編就為大家分享一篇關(guān)于關(guān)于C++動(dòng)態(tài)分配內(nèi)存的介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2021-08-08

最新評(píng)論