[英]How to get address of member function within the class without using class name for scope?
I want to know if it is possible to get the address of a member function within the class without using the class name for scope. 我想知道是否可以在不使用范围的类名称的情况下获得该类中成员函数的地址。
In the below example, inside main is the normal way of getting address, but inside func2, is there a way without using class name. 在下面的示例中,main内部是获取地址的常规方法,但func2内部是否存在不使用类名的方法。 The reason for asking for something like this is, in future if I change the class Name, I don't have to go and change inside.
要求这样的原因是,将来如果我更改类Name,则不必去内部进行更改。 eg:
例如:
Class A
{
void func1()
{
}
void func2()
{
/Address of func1/ = &func1; // something like this possible?
}
}
void main()
{
/Address of func1/ = &A::func1;
}
For non- static
member functions, 对于非
static
成员函数,
&std::remove_reference_t<decltype(*this)>::func1
is one way. 是一种方式。
For static
functions, use std::addressof
对于
static
函数,请使用std::addressof
std::addressof(func1);
which relies on the fact that the A::
is implicit. 这取决于
A::
是隐式的。
If func1
is a static
function, you can use std::addressof
to get its address. 如果
func1
是static
函数,则可以使用std::addressof
获取其地址。
class A
{
public:
static void func1()
{
}
void func2()
{
auto fptr = std::addressof(func1);
}
};
Note: This will not work on non- static
member functions. 注意:这不适用于非
static
成员函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.