#include <iostream>
using namespace std;
class A
{
public:
int memfunc(int i = 0)
{
cout << "A::memfunc" << endl;
}
static int clsfunc(int i = 0)
{
cout << "A::clsfunc" << endl;
}
};
int main()
{
int (*pcls)(int) = &A::clsfunc; // well
pcls = A::clsfunc; // well too, caus static-mem-func is a rvalue, will change to be a pointer
pcls(0); // ---- parameter is necessary!
(*pcls)(0); // ---- parameter is necessary!
A a;
int (A::*pmem)(int) = &A::memfunc; // well
// pmem = A::memfunc; // error, caus mem-func is a lvalue, cannot change to be a pointer
(a.*pmem)(0); // ---- parameter is necessary!
// (a.(*pmem))(0); // error, caus .* is a single operator
(&a->*pmem)(0); // ---- parameter is necessary!
// (&a->(*pmem))(0); // error, caus ->* is a single operator
return 0;
}
笔记:成员函数指针
C++类成员函数与静态成员函数的使用与区别
最新推荐文章于 2023-12-12 15:30:05 发布
本文详细介绍了C++中类成员函数与静态成员函数的使用方式,包括它们的区别以及如何正确地引用静态成员函数。通过实例代码演示了参数传递和调用过程。

2万+





