• C++学习笔记之 单例模式


    单例模式

    附:设计模式教程

    上面的教程来自C语言中文网,网址:http://m.biancheng.net/design_pattern/

    静态成员变量

    在类的定义中,它的成员(包括成员变量和成员函数),这些成员可以用关键字static修饰,声明为静态的,叫做静态成员
    不管类创建了多少个对象,静态成员只有一个拷贝,这个拷贝被所有属于这个类的对象共享

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // 静态成员变量
    
    // 1. 所有变量共享同一份静态成员
    // 2. 在编译阶段就分配了内存
    // 3. 必须在类内声明,类外初始化
    
    class Person
    {
    public:
        Person()
        {
            // m_Age = 13; // 不要在构造函数给静态成员变量初始化,已经晚了
        }
    
        Person(const Person &p)
        {
            cout << "Person的拷贝构造函数调用" << endl;
        }
    
        ~Person()
        {
            cout <<  "Person的析构函数调用" << endl;
        }
    
        static int m_Age;
    };
    
    int Person::m_Age = 13; // 类外初始化
    
    // 基本语法
    void test01()
    {
        Person p1;
        cout << "p1.m_Age = " << p1.m_Age << endl;
        Person p2;
        p2.m_Age = 14;
        cout << "p1.m_Age = " << p1.m_Age << endl;
    }
    
    void test02()
    {
        cout << endl;
        // 访问方式
        // 通过对象访问
        Person p;
        cout << "p.m_Age = " << p.m_Age << endl;
    
        // 通过类名访问
        cout << "Person.m_Age = " << Person::m_Age << endl;
    }
    
    int main()
    {
        test01();
        test02();
    
        return EXIT_SUCCESS;
    }
    
    p1.m_Age = 13
    p1.m_Age = 14
    Person的析构函数调用
    Person的析构函数调用
    
    p.m_Age = 14
    Person.m_Age = 14
    Person的析构函数调用
    

    注意:静态成员变量也是有访问权限的

    静态成员函数

    #include <iostream>
    
    using namespace std;
    
    class Person
    {
    public:
    
        static void func()
        {
            m_A = 1000;
            // m_B = 1000; 非静态成员变量无法修改
        }
    }
    
    int main()
    {
        Person p;
        p.func();
    
        return 0;
    }
    

    单例模式

    一个类只有一个实例,且该类能自行创建这个实例的一种模式

    单例模式-主席类

    #include <iostream>
    
    using namespace std;
    
    class ChairMan
    {
    // 防止创建多个主席对象,将构造函数私有化
    private:
        ChairMan()
        {
            cout << "主席类的创建" << endl;
        }
    
    public:
        // 在类中 声明主席的指针 在类外初始化主席对象
        static ChairMan *singleMan;
    
        // 提供对外接口 获取主席指针 而且是只读状态
        static ChairMan *getInstance()
        {
            return singleMan;
        }
    };
    
    ChairMan *ChairMan::singleMan = new ChairMan;
    
    void test01()
    {
        // 找到主席对象
        ChairMan *c1 = ChairMan::getInstance();
        ChairMan *c2 = ChairMan::singleMan;
    
        if (c1 == c2)
        {
            cout << "c1 == c2" << endl;
        }
        else
        {
            cout << "c1 != c2" << endl;
        }
    
        // ChairMan::getInstance() = nullptr; // error: lvalue required as left operand of assignment
    }
    
    int main()
    {
        cout << "main函数调用" << endl;
        test01();
    
        return 0;
    }
    
    主席类的创建
    main函数调用
    c1 == c2
    

    注:nullptr是C++中的NULL

    成员变量和成员函数分开储存

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Person1
    {
    public:
    };
    
    class Person2
    {
    public:
        int a;
    };
    
    class Person3
    {
    public:
        static int a;
    };
    
    int Person3::a = 10;
    
    void test01()
    {
        // 通过空类也可以实例化对象,每个对象都有独一无二的内存
        cout << "sizeof = " << sizeof(Person1) << endl;
    
        // 非静态成员属于类的对象上
        cout << "sizeof = " << sizeof(Person2) << endl;
        // 静态成员不属于类的对象上
        cout << "sizeof = " << sizeof(Person3) << endl;
    }
    
    int main()
    {
        test01();
    
        return 0;
    }
    
    sizeof = 1
    sizeof = 4
    sizeof = 1
    

    空类的大小是1

  • 相关阅读:
    Nhibernate 在一个项目中连接不同的数据库。记录
    sql2005 向2000导入数据。一些注意事项
    iis发布系统问题总结 .....关于handler的重写的应用
    ExecutorService 线程池
    Spring Boot 访问静态资源
    spring boot的核心注解
    日期处理工具类
    Json解析工具类
    (技能篇)Mysql在linux下的全量热备份
    Linux相关命令
  • 原文地址:https://www.cnblogs.com/zhujiangyu/p/13942510.html
Copyright © 2020-2023  润新知