static成员函数不可以直接访问成员变量和普通函数可以访问静态变量以及内存分配详解

关键在于是否给函数分配好空间了,以及是否给变量分配好空间了,分配了空间就可以用,没有分配空间就不可以用,static不管函数还是变量,不管属于类还是在类外,都是直接分配空间的,而类内的方法,和成员变量则没有分配,而类外的函数和变量是直接分配了空间的,所以可以直接用。
1.静态变量空间分配问题

#include<iostream>
using namespace std;
class Person{
    public:
        static int hand;
        static void hitPeople();
};
int main()
{
    cout<<'1'<<endl;
}

结果:
在这里插入图片描述

#include<iostream>
using namespace std;
class Person{
    public:
        static int hand = 0;
        static void hitPeople();
};
int main()
{
    cout<<'1'<<endl;
}

结果:报错

#include<iostream>
using namespace std;
class Person{
    public:
        static int hand;
        static void hitPeople();
};
int Person::hand;
int main()
{
    cout<<'1'<<endl;
}

结果:
在这里插入图片描述

#include<iostream>
using namespace std;
class Person{
    public:
        int hand = 0;
        static void hitPeople();
};
int main()
{
    cout<<'1'<<endl;
}

结果:
在这里插入图片描述
原因:静态变量只能在类外进行分配空间,原因是如果在类内就进行了分配空间,实例化对象时候,就会多次分配内存,没有起到静态变量的作用,因此,c++在设计时,设计不在类外分配空间就不通过。所以静态变量是需要在类内声明,类外定义的。而静态函数则在类内直接定义也可以,因为不需要分配空间。
2.静态函数可以直接访问静态变量,静态函数不可以直接访问非静态变量,但可以通过将类实例化为对象之后,静态函数去访问对象的非static成员变量。
普通的成员函数通过实例化类调用普通变量

#include<iostream>
using namespace std;
class Person{
    public:
        int hand = 1;
        void hitPeople()
        {
            cout<<hand<<endl;
        }
};
int main()
{
    cout<<"类的大小为:"<<sizeof(Person)<<endl;
    Person person1;
    person1.hitPeople();
}

结果:
在这里插入图片描述
普通的成员函数通过实例化类调用静态变量

#include<iostream>
using namespace std;
class Person{
    public:
        static int hand;
        void hitPeople()
        {
            cout<<hand<<endl;
        }
};
int Person::hand = 1;
int main()
{
    cout<<"类的大小为:"<<sizeof(Person)<<endl;
    Person person1;
    person1.hitPeople();
}

结果:
在这里插入图片描述
静态函数可以直接访问静态变量:

#include<iostream>
using namespace std;
class Person{
    public:
        static int hand;
        static void hitPeople()
        {
            cout<<hand<<endl;
        }
};
int Person::hand = 1;
int main()
{
    cout<<"类的大小为:"<<sizeof(Person)<<endl;
    Person::hitPeople();
}

结果:
在这里插入图片描述
静态函数不可以直接访问非静态变量:

#include<iostream>
using namespace std;
class Person{
    public:
        int hand = 1;
        static void hitPeople()
        {
            cout<<hand<<endl;
        }
};
//int Person::hand
int main()
{
    cout<<"类的大小为:"<<sizeof(Person)<<endl;
    Person person1;
    person1.hitPeople();
}

结果:报错
在这里插入图片描述
静态函数可以访问类实例化后对象的非静态变量
#include
using namespace std;
class Person{
public:
int hand = 1;
static void hitPeople(int a)
{
cout<<a<<endl;
}
};
//int Person::hand
int main()
{
cout<<“类的大小为:”<<sizeof(Person)<<endl;
Person person1;
Person::hitPeople(person1.hand);
}
在这里插入图片描述
归咎根本原因是因为,static函数和普通函数不同,因为它不属于任何一个对象,没有this指针,this的含义是某一个对象的起始地址,该对象的其它成员通过和this的相对地址找到,静态函数只有一个地址,当对象>=2时,无法通过对象的相对地址寻找(不知道以哪个对象为起始地址),所以静态函数是没有this指针的,无法直接的通过this指针去访问普通的成员变量,只能通过传参的形式,而静态成员函数在访问静态变量时由于即使没有this指针,也能确定就是这个静态变量,不管是哪个对象,都只有这一个名字的静态变量,也就不需要this指针,就可以直接调用这个确定的成员变量。

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值