【C++】静态成员函数

时间:2025-03-25 10:55:27

【C++】 静态成员函数

文章目录

  • 【C++】 静态成员函数
    • 一 、静态成员函数概述
    • 二、静态函数不可以声明为虚函数
    • 三、静态成员函数不包含 this 指针
    • 四、静态成员函数不可以声明为 const
    • 五、static 关键字不构成重载
    • 六、静态成员函数的地址与绑定

一 、静态成员函数概述


类中的成员函数,若未使用类中的成员变量,则可声明为静态成员函数,函数声明前加上 static 关键字。

静态成员函数不属于对象,属于类。静态成员函数不包含编译器提供的 this 指针,在类没有实例化对象前就存在,由于没有 this 指针,所以也就没有特定的成员变量可以使用。

静态成员函数与成员函数用法上的主要不同为:
静态成员函数可以独立访问,调用静态成员函数时,不需要实例化任何对象。只需要使用 类名 + 命名空间标识符 (::) 加函数名即可调用。

ClassName::StaticMemberFunction();

普通成员函数则需要通过对象调用,首先需要实例化一个对象。

ClassName obj;
obj.MemberFunction();

二、静态函数不可以声明为虚函数


如果静态成员函数加上 virtual 关键字,则会编译报错。再者 virtual 的使用,依赖对象内部的 vptr 虚表指针,但静态函数不属于某个具体对象,无法访问其虚表指针。

class StaticMemberFuncTest
{
public:
	static virtual void test() {};
};

编译报错为:virtual 关键字不可以与 static 一起使用。

 error C2216: 'virtual' cannot be used with 'static'

三、静态成员函数不包含 this 指针


如概述所说,静态成员函数不属于某个具体的对象,而属于类,所以不包含 this 指针。从定义上来讲静态成员函数不使用类中的非静态成员变量,非静态成员变量只有在函数实例化时才会分配内存,this 指针的地址为实例化对象的起始地址。

成员函数既可以调用静态成员变量,也可以调用非静态成员变量;而静态成员函数仅可调用静态成员变量。

示例代码

class StaticMemberFuncTest
{
public:
	static StaticMemberFuncTest * test() {
		return this;
	};
};

编译报错为:

error C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers

this 关键字只能在非静态成员函数或非静态数据成员初始化中引用。

四、静态成员函数不可以声明为 const


未修改成员变量的函数可以声明为常函数,加上 const 关键字,但静态成员函数不可以。

class StaticMemberFuncTest
{
public:
	static void Test() const {
		//;
	};
};
error C2272: 'Test': modifiers not allowed on static member functions

静态成员函数上不允许使用访问限定词,如 const , volatile 等。

五、static 关键字不构成重载


如果一个静态成员函数与一个成员函数的函数名相同,且参数类型列表相同,此两者不会构成重载。

代码示例

class StaticMemberFuncTest
{
public:
	static void Test() {}
	void Test() {}
};

编译报错

error C2686: cannot overload static and non-static member functions with the same parameter types
note: could be 'void StaticMemberFuncTest::Test(void)'
note: or       'void StaticMemberFuncTest::Test(void)'

不能用相同的参数类型重载静态和非静态成员函数。

六、静态成员函数的地址与绑定


对于类

class StaticMemberFuncTest
{
public:
	static int StaticFunc() {/**/};
	int MemberFunc() {/**/};
};
  1. 静态成员函数在存储函数地址时可以用普通的函数指针
// 静态成员函数指针
int (*funcptr1)() = &StaticMemberFuncTest::StaticFunc;
// 成员函数指针
int (StaticMemberFuncTest::*funcptr2)() = &StaticMemberFuncTest::MemberFunc;
  1. 在使用 STL 的 bind 时,不需要传入类成员函数的指针
// 绑定静态成员函数
std::bind(&StaticMemberFuncTest::StaticFunc);
// 绑定成员函数
std::bind(&StaticMemberFuncTest::MemberFunc, this);

参考链接:

  • /view/

  • /p/28668742