Function returning pointer to itself?

前端 未结 4 1792
闹比i
闹比i 2020-12-28 14:35

Is it possible in C++ to write a function that returns a pointer to itself?

If no, suggest some other solution to make the following syntax work:

som         


        
4条回答
  •  心在旅途
    2020-12-28 15:19

    Of course it is possible, just look the following code:

    
    #include 
    
    typedef void * (*func)();
    
    void * test()
    {
        printf("this is test\n");
        return (void *)&test;
    }
    
    int main()
    {
        (*(func)test())();
    }
    

    The result is :

    
    user@linux:~/work/test> gcc test_func.cc -o test          
    user@linux:~/work/test> ./test
    this is test
    this is test
    

提交回复
热议问题