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++中結(jié)構(gòu)體的類型定義和初始化以及變量引用
這篇文章主要介紹了C++中結(jié)構(gòu)體的類型定義和初始化以及變量引用,是C++入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
C語言數(shù)據(jù)結(jié)構(gòu)通關(guān)時間復雜度和空間復雜度
對于一個算法,其時間復雜度和空間復雜度往往是相互影響的,當追求一個較好的時間復雜度時,可能會使空間復雜度的性能變差,即可能導致占用較多的存儲空間,這篇文章主要給大家介紹了關(guān)于C語言時間復雜度、空間復雜度的相關(guān)資料,需要的朋友可以參考下2022-04-04
在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法
經(jīng)常會遇到類型轉(zhuǎn)換,本文主要介紹了C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06

