亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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++中結構體的類型定義和初始化以及變量引用

    C++中結構體的類型定義和初始化以及變量引用

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

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

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

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

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

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

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

    利用Matlab繪制地圖的超詳細教程

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

    C語言數(shù)據結構通關時間復雜度和空間復雜度

    對于一個算法,其時間復雜度和空間復雜度往往是相互影響的,當追求一個較好的時間復雜度時,可能會使空間復雜度的性能變差,即可能導致占用較多的存儲空間,這篇文章主要給大家介紹了關于C語言時間復雜度、空間復雜度的相關資料,需要的朋友可以參考下
    2022-04-04
  • 清除3389遠程登錄日志

    清除3389遠程登錄日志

    這篇文章主要介紹了清除3389遠程登錄日志示例,需要的朋友可以參考下
    2014-01-01
  • C++算法學習之貪心算法的應用

    C++算法學習之貪心算法的應用

    貪心算法是指,在對問題求解時,總是做出在當前看來是最好的選擇。本文為大家準備了幾個示例,從而能深入了解貪心算法的應用,需要的可以參考一下
    2022-05-05
  • 在C++中把字符串轉換為整數(shù)的兩種簡單方法

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

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

    C++動態(tài)內存管理詳解

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

最新評論