0
코드 :비 정적 멤버 함수 reinterpret_cast 실패
#include <iostream>
using namespace std;
struct item
{
int f1() {}
double f2() {}
static int g1() {}
static double g2() {}
void f0();
};
void item::f0()
{
auto c1 = reinterpret_cast<decltype(f2)>(f1);
auto c2 = reinterpret_cast<decltype(g2)>(g1);
auto c3 = reinterpret_cast<decltype(&f2)>(f1);
auto c4 = reinterpret_cast<decltype(&g2)>(g1);
}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
오류 메시지 :
main.cpp|17|error: invalid use of non-static member function|
main.cpp|18|error: invalid cast from type ‘int (*)()’ to type ‘double()’|
main.cpp|20|error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&item::f2’ [-fpermissive]|
main.cpp|20|error: invalid use of member function (did you forget the ‘()’ ?)
내 질문 : 인수로 전달 멤버 함수가 자동으로 포인터로 변환, 그래서 내가 캐스팅 시도 포인터에 대한 인수이지만 여전히 실패했습니다. 비 정적 멤버 함수가 모든 상황에서 작동하지 않는 이유를 모르겠습니다.
나는 c1을 함수로 만들고 싶습니다. reinterpret_cast는 int를 double로 변환 할 수 없습니다. – lnvm
@ Gr.Five, 업데이트 된 답변을 참조하십시오. –
@R Sahu, 멋지다! 그것은 효과가 있지만 왜 설명해 주시겠습니까? – lnvm