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
[40458] Re:원하시는 답변은 아니랍니다.. -_-;;
소리바람.OJ [phonon] 1004 읽음    2005-06-08 16:38
저도 비슷한 자료를 올립니다.

http://www.devarticles.com/c/a/Cplusplus/Function-Pointers-part-1/3/

   
Function Pointers, part 1 - Defining and Using Pointers to Templated Functions
( Page 4 of 4 )

Let’s try to make this even more interesting and take a look at how we define pointers to templated functions. First we need a templated function to point at:

    template T Inc(T var) {

    return ++var;

    }

Unfortunately the C++ standard doesn’t allow us to make template declarations in the scope of function bodies, so we cannot define a pointer to a templated function like this in main():

    template T (*IncPtr)(T) = &Inc;

It is important to understand how compilers instantiate templates as well. They only instantiate templated code when it is needed, and they will not even detect a syntax error if the templated function is never called (a lot of clever and tricky meta template programming makes good use of this compiler behavior). It doesn’t make sense, and hopelessly complicates things for compiler builders, when you try to declare a templated pointer at a point where the compiler is hard at work instantiating code (as in function bodies). When you make a function call, data is going to be moved onto the memory stack, and the compiler needs to know which parameters to use. By trying to template the pointer, we are denying it that very information, and it won’t be able to continue the compilation.

So what is it we can do with the templated function Inc() we have? When we tell the compiler how we are planning to use the function, defining a function pointer to it works fine:

    int main(int argc, char *argv[]) {

    // Use Inc to increase an integer

    int (*incN)(int) = &Inc;

    int nres = incN(1);

    (void)printf(“result is: %d\n”, nres);

    // Use Inc to increase a float

    float (*incF)(float) = &Inc;

    float fres = incF(1.f);

    (void)printf(“result is: %f\n”, fres);

    }

When you run this example you will see that it works fine.

So is there no way we can delay the type definition of the function we want to define a function pointer to? Well, there is one location where this is possible: inside a templated function:

    template

    T Foo(T var) {

    T (*incPtr)(T) = &Inc ;

    return incPtr(var); }

Adding the following line to our example shows that it works fine:

    int foores = Foo (1);

    (void)printf(“result is: %d\n”, result);

When you think about it, it actually makes sense, because by the time Foo is instantiated, typename T is known by the compiler, and it has no problem instantiating the function pointer to Inc.

There is much more to function pointers than actually meets the eye. In the next article I will take a look at class member function pointers and place them in the same context I have done with the regular function pointers in this article. Finally, in the third article you will find a callback implementation example, function pointers mixed with calling conventions and an introduction to C++ functors.

수야!╋ 님이 쓰신 글 :
: ㅋ_ㅋ;;
:
: 저도 자주 사용하지는 않는 거라서 잘은 모른답니다... -_-;;
:
: 답변을 한번 써볼까 시도했는데 저 스스로 이해하지 못한부분이 있어서 답변이 나오질 않는군요
:
: 원하시는 것은 보통 일컫기를 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 1004 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.