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

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

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

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

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

    public:
        // 靜態(tài)成員函數(shù)(才可以調用靜態(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)命名空間內的函數(shù),實例化一個類,并調用函數(shù)
    const std::string& getNameSpace(){
        return ThisNode::instance().getNameSpace();
    }
    const std::string& getName(){
        return ThisNode::instance().getName();
    }

};
  • 實現(xiàn)命名空間函數(shù)
    首先調用的是類的靜態(tài)成員函數(shù)實例化唯一對象,然后調用對象中的方法;
// 實現(xiàn)命名空間內的函數(shù),實例化一個類,并調用函數(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;
}

全部代碼

#include<string>
#include<iostream>

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

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

    public:
        // 靜態(tài)成員函數(shù)(才可以調用靜態(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)命名空間內的函數(shù),實例化一個類,并調用函數(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;
}

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

相關文章

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

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

    這篇文章主要給大家介紹了關于C語言堆棧幀的相關資料,堆棧幀 (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ù)結構,用一組地址任意的存儲單元存放線性表中的數(shù)據(jù)元素。這篇文章主要介紹了C語言單鏈表的實現(xiàn) 的相關資料,需要的朋友可以參考下
    2016-04-04
  • C++可變參數(shù)模板深入深剖

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

    個可變參數(shù)模板(variadic template)就是一個接受可變數(shù)目參數(shù)的函數(shù)模板或類模板,下面這篇文章主要給大家介紹了關于C++可變參數(shù)模板的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    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++初學者更好地掌握C++的邏輯語句用法,需要的朋友可以參考下
    2015-09-09
  • C++中static修飾符的詳解及其作用介紹

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

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

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

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

    如何通過UltraEdit解析BMP文件內部結構(BMP位圖基礎)

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

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

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

最新評論

科技| 弥勒县| 罗城| 乐清市| 江陵县| 南皮县| 宜兰市| 扶绥县| 天全县| 汤原县| 孝义市| 沈丘县| 东港市| 达拉特旗| 东明县| 景德镇市| 台北县| 利辛县| 镇巴县| 乌拉特前旗| 突泉县| 柳河县| 新民市| 集安市| 合江县| 辛集市| 行唐县| 奇台县| 营山县| 淮滨县| 新野县| 江阴市| 买车| 广南县| 北流市| 丰顺县| 新沂市| 灵丘县| 萨迦县| 西藏| 海盐县|