c++类的静态成员变量、静态成员函数、const修饰的静态成员变量、静态成员实现单例模式、this指针的应用

文章详细介绍了C++中静态成员变量和静态成员函数的概念和使用,包括它们的内存分配、访问方式以及作用。此外,还讨论了const修饰的静态成员变量的特性,如只读和内存中的唯一性。文章通过实例展示了单例模式的实现,强调了确保类只有一个实例的重要性。最后,提到了const修饰的成员函数,这种函数不能修改对象的内容。
摘要由CSDN通过智能技术生成

静态成员变量

  • 静态成员变量
    1、静态成员变量在内存中只有一份,多个对象共享一个静态变量
    2、静态成员变量必须类内声明,类外定义
    3、静态成员变量可以通过类的作用域访问
    4、静态成员变量可以通过类的对象访问
#include <iostream>
#include <string>
using namespace std;

class person
{
public:
    int a;

    static int b;  //静态成员变量在内存中只有一份
};
int person::b = 10;

void test01()
{
    cout << person::b <<endl;   // 静态成员变量可以通过类的作用域访问

    person p1;
    p1.b = 100;
    cout << p1.b << endl;   // 静态成员变量可以通过类的对象访问

    person p2;
    cout << p2.b << endl;   // 多个对象共享一个静态变量
}

int main()
{
    test01();
    return 0;
}

静态成员函数

  • 静态成员函数
    1、静态成员函数,可以访问静态成员变量,不能访问普通的成员变量
    2、可以通过类的作用域访问静态成员函数
    3、可以通过对象访问静态成员函数
#include <iostream>
#include <string>
using namespace std;

class person
{
public:
    int a;

    static int b;

    void show()
    {
        cout << a<<" " << b<<endl;
    }
    static void static_show()
    {
        cout << " " << b<<endl;
    }
};
int person::b = 10;

void test01()
{
    person::static_show();

    person p1;
    p1.static_show();


}

int main()
{
    test01();
    return 0;
}

const修饰的静态成员变量


const修饰的静态成员变量
1、const修饰的静态成员变量,保存在常量区,只读的,在内存中只有一份
2、const修饰的静态成员变量可以在类内定义且初始化
3、const修饰的静态成员变量可以通过类的作用域访问
4、const修饰的静态成员变量可以通过对象访问
5、静态成员函数可以访问const修饰的静态成员变量

#include <iostream>
#include <string>
using namespace std;

const int num = 10; // const修饰的全局变量保存在常量区 不可更改
class person
{
public:
    int a;
    static int b;        // 静态成员变量,在编译阶段就分配内存,存在静态全局区
    const static int c = 1000;  // const修饰的静态成员变量,是保存在常量区,不可修改(只读),在内存中只有一份

};
int person::b = 10;  // 类中成员变量的定义

void test01()
{
    cout << person::c<<endl;

    person p1;
    cout << p1.c<< endl;
}

int main()
{
    test01();
    return 0;
}

单例模式举例


静态成员实现 单例模式

一个类只能实例化出一个对象 叫单例模式

单例模式实现的步骤
1、将无参构造私有化
2、将拷贝构造私有化
3、定义一个静态的成员指针变量,指向new出来的一个唯一对象
4、将静态的成员指针变量私有化,提供获得唯一对象的地址的静态接口

#include <iostream>
#include <string>
using namespace std;

class Feifei
{
public:
    int age;
    int yanzhi;

    static Feifei * instance()
    {
        return single;
    }

private:
    static Feifei *single ;

    Feifei() {}  // 无参构造私有化
    Feifei(const Feifei &p) {}   // 拷贝构造 私有化
};

Feifei * Feifei::single = new Feifei;

void test02()
{
    Feifei * p = Feifei::instance();
    p->age = 10;
    p->yanzhi = 20;

    Feifei * p1 = Feifei::instance();
    cout << p1->age <<" " << p1->yanzhi << endl;   // 10 20   p1和p是同一个对象

    // Feifei::single->age =20;
    // Feifei::single->yanzhi = 100;

    // Feifei p1(*Feifei::single); // 调用拷贝构造实例化出一个对象  不让调用拷贝构造,将拷贝构造私有化

    // Feifei::single = NULL;  // 当前对象可能会被设置NULL  想办法:让不允许修改对象
}

int main()
{
    test02();
    return 0;
}

this指针举例

this指针的应用

#include <iostream>
#include <string>
using namespace std;

class person
{
public:
    person(int age, string name)
    {
        this->age = age;
        this->name = name;
    }
    void show ()
    {
        cout << age<< " " << name<< endl;
    }
    person person_add(person &p2)
    {
        person p(this->age+p2.age, this->name+p2.name);
        return p;
    }

    int age;
    string name;
};

void test02()
{
    /*
    person *p1 =new person(10,"hello");
    p1->show();   // 10 hello

    person *p2 = new person(20, "world");
    p2->show();   // 20 world

    person p3 = p1->person_add(*p2);
    p3.show();    // 30 helloworld
    */


    person p1(10, "hello");
    p1.show();   // 10 hello
    person p2(20, "world");
    p2.show();   // 20 world
    person p3 = p1.person_add(p2);
    p3.show();   // 30 helloworld

}

int main()
{
    test02();
    return 0;
}

const修饰的类成员函数

const修饰的成员函数
1、函数后面加const 称为:常函数;const type const this
2、const修饰的成员函数,不能通过this指针修改this指针指向的对象内容

#include <iostream>
#include <string>
using namespace std;

class person
{
public:
    person(int age, string name)
    {
        this->age = age;
        this->name = name;
    }
    void show ()
    {
        cout << age<< " " << name<< endl;
    }
    person person_add(person &p2) const       // 函数后面加const 称为:常函数;const type const this
    {
        // this->age = 11;                    // const修饰的成员函数,不能通过this指针修改this指针指向的对象内容
        person p(this->age+p2.age, this->name+p2.name);
        return p;
    }

    int age;
    string name;
};

void test02()
{
    /*
    person *p1 =new person(10,"hello");
    p1->show();   // 10 hello

    person *p2 = new person(20, "world");
    p2->show();   // 20 world

    person p3 = p1->person_add(*p2);
    p3.show();    // 30 helloworld
    */


    person p1(10, "hello");
    p1.show();   // 10 hello
    person p2(20, "world");
    p2.show();   // 20 world
    person p3 = p1.person_add(p2);
    p3.show();   // 30 helloworld

}

int main()
{
    test02();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值