最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

  • 聲明命名空間及函數(shù)
namespace test{
    const std::string& getName()和();
    const std::string& getNameSpace();
}
  • 命名空間內(nèi)實現(xiàn)單例類
    實現(xiàn)一個單例類,構(gòu)造函數(shù)要為private,自身對象為private
    靜態(tài)成員函數(shù)(才可以調(diào)用靜態(tài)成員變量)
namespace test{
    // 實現(xiàn)一個單例類,構(gòu)造函數(shù)要為private,自身對象為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ù):實例化類
         * 返回值:ThisNode&
        */
        static ThisNode& instance(){
            if(thisNode==nullptr){
                std::cout << "沒有" <<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;

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

};
  • 實現(xiàn)命名空間函數(shù)
    首先調(diào)用的是類的靜態(tài)成員函數(shù)實例化唯一對象,然后調(diào)用對象中的方法;
// 實現(xiàn)命名空間內(nèi)的函數(shù),實例化一個類,并調(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)的兩個函數(shù)
namespace test{
    const std::string& getName()和();
    const std::string& getNameSpace();
}

namespace test{
    // 實現(xiàn)一個單例類,構(gòu)造函數(shù)要為private,自身對象為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ù):實例化類
         * 返回值:ThisNode&
        */
        static ThisNode& instance(){
            if(thisNode==nullptr){
                std::cout << "沒有" <<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;

    // 實現(xiàn)命名空間內(nèi)的函數(shù),實例化一個類,并調(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++使用單例模式實現(xiàn)命名空間函數(shù)的文章就介紹到這了,更多相關(guān)c++命名空間函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言堆棧幀的介紹與創(chuàng)建

    C語言堆棧幀的介紹與創(chuàng)建

    這篇文章主要給大家介紹了關(guān)于C語言堆棧幀的相關(guān)資料,堆棧幀 (stack frame)( 或活動記錄 (activation Tecord)) 是一塊堆棧保留區(qū)域,用于存放被傳遞的實際參數(shù)、子程序的返回值、局部變量以及被保存的寄存器,需要的朋友可以參考下
    2021-08-08
  • 用C實現(xiàn)添加和讀取配置文件函數(shù)

    用C實現(xiàn)添加和讀取配置文件函數(shù)

    本篇文章是對用C語言實現(xiàn)添加和讀取配置文件函數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言單鏈表的實現(xiàn)

    C語言單鏈表的實現(xiàn)

    單鏈表是一種鏈式存取的數(shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲單元存放線性表中的數(shù)據(jù)元素。這篇文章主要介紹了C語言單鏈表的實現(xiàn) 的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • C++可變參數(shù)模板深入深剖

    C++可變參數(shù)模板深入深剖

    個可變參數(shù)模板(variadic template)就是一個接受可變數(shù)目參數(shù)的函數(shù)模板或類模板,下面這篇文章主要給大家介紹了關(guān)于C++可變參數(shù)模板的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • c語言printf函數(shù)的使用詳解

    c語言printf函數(shù)的使用詳解

    本篇文章是對c語言中printf函數(shù)的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++條件及循環(huán)語句的綜合運用實例

    C++條件及循環(huán)語句的綜合運用實例

    這篇文章主要介紹了C++條件及循環(huán)語句的綜合運用實例,能夠幫助C++初學(xué)者更好地掌握C++的邏輯語句用法,需要的朋友可以參考下
    2015-09-09
  • C++中static修飾符的詳解及其作用介紹

    C++中static修飾符的詳解及其作用介紹

    這篇文章主要介紹了C++中static修飾符的詳解及其作用介紹,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • C++六大默認成員函數(shù)的實現(xiàn)

    C++六大默認成員函數(shù)的實現(xiàn)

    C++中的六大默認成員函數(shù)包括默認構(gòu)造函數(shù)、默認析構(gòu)函數(shù)、默認拷貝構(gòu)造函數(shù)、默認拷貝賦值運算符、移動構(gòu)造函數(shù)和移動賦值運算符,本文就來介紹一下這些函數(shù)的使用,感興趣的可以了解一下
    2025-02-02
  • 如何通過UltraEdit解析BMP文件內(nèi)部結(jié)構(gòu)(BMP位圖基礎(chǔ))

    如何通過UltraEdit解析BMP文件內(nèi)部結(jié)構(gòu)(BMP位圖基礎(chǔ))

    我們先打開畫圖隨便畫一幅圖并采用24位bmp圖像格式保存,就得到了一張24位真彩色的位圖,下面我們來詳細分析bmp位圖的各個組成部分,感興趣的朋友跟隨小編一起看看吧
    2021-08-08
  • C語言代碼實現(xiàn)簡易掃雷

    C語言代碼實現(xiàn)簡易掃雷

    這篇文章主要為大家詳細介紹了C語言代碼實現(xiàn)簡易掃雷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01

最新評論

岳普湖县| 沙河市| 水富县| 射阳县| 岳西县| 澄江县| 岫岩| 乐安县| 马边| 安阳市| 平安县| 秦皇岛市| 宜城市| 甘洛县| 长阳| 珠海市| 扎囊县| 贵德县| 霍邱县| 肇源县| 龙岩市| 桐乡市| 乾安县| 灵寿县| 琼结县| 都江堰市| 铜山县| 鸡西市| 沂源县| 大安市| 潢川县| 柳江县| 鄂托克旗| 曲沃县| 永昌县| 南靖县| 富川| 汪清县| 龙门县| 保德县| 鱼台县|