您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“c++怎么使用單例模式實現(xiàn)命名空間函數(shù)”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
本案例實現(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)一個單例類,構造函數(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)命名空間內(nèi)的函數(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)命名空間內(nèi)的函數(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> // 聲明命名空間內(nèi)的兩個函數(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)命名空間內(nèi)的函數(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ù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。