C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[40456] 원하시는 답변은 아니랍니다.. -_-;;
수야!╋ [sooya23] 1040 읽음    2005-06-08 16:02
ㅋ_ㅋ;;

저도 자주 사용하지는 않는 거라서 잘은 모른답니다... -_-;;

답변을 한번 써볼까 시도했는데 저 스스로 이해하지 못한부분이 있어서 답변이 나오질 않는군요

원하시는 것은 보통 일컫기를 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형 클래스를 가르키는 함수포인터 선언을 하고파요...

+ -

관련 글 리스트
40454 아무 클래스 멤버 함수를 가르키는 함수포인터 선언방법이 있을까요? EmptySpear 1615 2005/06/08
40468     Re:아무 클래스 멤버 함수를 가르키는 함수포인터 선언방법이 있을까요? Batman 1713 2005/06/08
40456     원하시는 답변은 아니랍니다.. -_-;; 수야!╋ 1040 2005/06/08
40458         Re:원하시는 답변은 아니랍니다.. -_-;; 소리바람.OJ 1005 2005/06/08
40460             Re:Re:원하시는 답변은 아니랍니다.. -_-;; EmptySpear 922 2005/06/08
40464                 원하시는 답변일겁니다.. -_-;; 수야!╋ 1186 2005/06/08
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.