设为首页 加入收藏

TOP

C++——使用单例模式实现命名空间函数
2023-07-23 13:32:40 】 浏览:27
Tags:单例模 空间函

本案例实现一个test命名空间,此命名空间内有两个函数,分别为getName()和getNameSpace();

  1. 声明命名空间及函数
namespace test{
    const std::string& getName()和();
    const std::string& getNameSpace();
}
  1. 命名空间内实现单例类
    实现一个单例类,构造函数要为private,自身对象为private
    静态成员函数(才可以调用静态成员变量)
namespace test{
    // 实现一个单例类,构造函数要为private,自身对象为private
    class ThisNode{
    private:
        std::string name_;
        std::string namespace_;
        static ThisNode *thisNode;
        ThisNode():name_("empty"),namespace_("namespace"){};

    public:
        // 静态成员函数(才可以调用静态成员变量)
        /**
         * 函数:实例化类
         * 返回值:ThisNode&
        */
        static ThisNode& instance(){
            if(thisNode==nullptr){
                std::cout << "没有" <<std::endl;
                thisNode = new ThisNode();
                return *thisNode;
            }else{
                std::cout << "有" <<std::endl;
                return *thisNode;
            }
        }
        // 普通成员函数
        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_;
        }
    };
    // 初始化静态成员
    ThisNode *ThisNode::thisNode = nullptr;

    // 实现命名空间内的函数,实例化一个类,并调用函数
    const std::string& getNameSpace(){
        return ThisNode::instance().getNameSpace();
    }
    const std::string& getName(){
        return ThisNode::instance().getName();
    }

};
  1. 实现命名空间函数
    首先调用的是类的静态成员函数实例化唯一对象,然后调用对象中的方法;
// 实现命名空间内的函数,实例化一个类,并调用函数
const std::string& getNameSpace(){
	return ThisNode::instance().getNameSpace();
}
const std::string& getName(){
	return ThisNode::instance().getName();
}
  1. 调用
int main(){
    // 使用
    test::getNameSpace();
    test::getName();
    return 0;
}

image

全部代码

#include<string>
#include<iostream>

// 声明命名空间内的两个函数
namespace test{
    const std::string& getName()和();
    const std::string& getNameSpace();
}

namespace test{
    // 实现一个单例类,构造函数要为private,自身对象为private
    class ThisNode{
    private:
        std::string name_;
        std::string namespace_;
        static ThisNode *thisNode;
        ThisNode():name_("empty"),namespace_("namespace"){};

    public:
        // 静态成员函数(才可以调用静态成员变量)
        /**
         * 函数:实例化类
         * 返回值:ThisNode&
        */
        static ThisNode& instance(){
            if(thisNode==nullptr){
                std::cout << "没有" <<std::endl;
                thisNode = new ThisNode();
                return *thisNode;
            }else{
                std::cout << "有" <<std::endl;
                return *thisNode;
            }
        }
        // 普通成员函数
        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_;
        }
    };
    // 初始化静态成员
    ThisNode *ThisNode::thisNode = nullptr;

    // 实现命名空间内的函数,实例化一个类,并调用函数
    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++写.. 下一篇从0开始学习c++

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目