ㅋ_ㅋ;;
저도 자주 사용하지는 않는 거라서 잘은 모른답니다... -_-;;
답변을 한번 써볼까 시도했는데 저 스스로 이해하지 못한부분이 있어서 답변이 나오질 않는군요
원하시는 것은 보통 일컫기를 FunClass 라고 합니다.
그래서 네이버를 검색해봤는데
http://cpptips.hyperformix.com/cpptips/static_f_ptr 이곳에 아주 간략한 내용이 있군요
템플릿으로 구현하는 방법도 있긴한데 -_-;; 앞서 말씀드린데로 이해하질 못해서 -_-;;
싸이트가 소멸될까바 내용을 퍼왔습니다.
-_-;;
TITLE: Calling static class members via pointers
PROBLEM: doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott)
Is it possible to call a static member function via a pointer without having
an instance of the class present at the time?
The following code is clearly wrong:
class FunClass {
public:
static int foo();
};
int FunClass::foo() { return 1; }
typedef /* static? */ int (FunClass::*ClassFun)();
main() {
ClassFun fun = FunClass::foo; // incompatible -- why?
printf("%d\n", (FunClass::*fun)()); // parse error here
}
RESPONSE: adk@Warren.MENTORG.COM (Ajay Kamdar)
Yes, it is possible to call a static member function via a pointer
without having an instance of the class present at the time.
This typedef is wrong. A pointer to a static member function is
declared as if the function was not a member function.
See example below.
This is the proper way to do what you want:
#include <iostream.h>
class FunClass {
public:
static int foo();
};
int FunClass::foo() {return 1;}
// static member function
int bar() {return 0;}
// not a member function. Yet has the same signature as FunClass::foo
typedef int (*PF)();
main()
{
PF fun = FunClass::foo;
cout << fun() << endl; // will print 1
fun = bar;
cout << fun() << endl; // will print 0
}
EmptySpear 님이 쓰신 글 :
: 안녕하세요..
:
: 아무 클래스 멤버 함수를 가르키는 함수포인터 선언방법이 있을까요?
:
: typedef void (Test::*FuntPtr)(); -> 이건 Test클래스의 멤버함수를 가르키는 함수포인터지요?
:
: 그런데 Test라는 특정클래스와 상관없이 어떤 클래스라도 가르키는 함수포인터를 어떻게 선언할까요?
:
: 단 BCB전용인(??)__closure키워드 사용치 말구 표준 C++로 ...템플리트를 이용하면 될것 같은데 ...
:
: 미천한 지식이라 잘 모르겠네요..꼭 알려주세요
:
: template <class T> void (T::*FuncPtr)(); 이런식으로 T형 클래스를 가르키는 함수포인터 선언을 하고파요...