1.静态函数成员的访问权限和继承规则和普通成员无区别;不同之处,普通函数成员第一个参数为隐含 this 参数,而静态函数成员没有隐含 this参数;
2.构造函数,析构函数以及虚函数都有this参数,此外,若成员参数表后面出现 const 或 volatile,则函数必包含隐含的 this 参数,这些函数不能定义为静态函数;
其二者区别
静态成员函数与普通成员函数的区别:
- 静态成员函数没有 this 指针,只能访问静态成员(包括静态成员变量和静态成员函数)。
- 普通成员函数有 this 指针,可以访问类中的任意成员;而静态成员函数没有 this 指针。
#include <iostream>
using namespace std;
class Box
{
public:
static int objectCount;
// 构造函数定义
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// 每次创建对象时增加 1
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
static int getCount()
{
return objectCount;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 初始化类 Box 的静态成员
int Box::objectCount = 0;
int main(void)
{
// 在创建对象之前输出对象的总数
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // 声明 box1
Box Box2(8.5, 6.0, 2.0); // 声明 box2
// 在创建对象之后输出对象的总数
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}
执行结果
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2
参考:https://www.runoob.com/cplusplus/cpp-static-members.html