0

呼び出しから返される値が何であるかは明確ではありません:

&next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr

それらはすべて私に価値を与えてくれるようです1。どういう意味ですか?

また、どのように宣言しますか

int (*return_f())(char)

typedef を使用せずにパラメーターを受け取るには?

#include <iostream>

int next(int n){
    return n+99;    
}

// returns pointer to a function
typedef int (*fptr)(int);               // using typdef
fptr return_func_ptr(){
    return next;    
}

int f(char){
    return 0;
}
int (*return_f())(char){                // how do you pass a parameter here?

    // std::cout << "do something with " << param << std::endl;
    return f;
}



int main()
{

    int x = 5;

    // p points to x
    int *p = &x;
    std::cout << "x=" << x << std::endl;        // 5,               value of x
    std::cout << "&x=" << &x << std::endl;      // 0x7fff6447a82c,  address of x
    std::cout << "p=" << p << std::endl;        // 0x7fff6447a82c,  value of p is address of x
    std::cout << "*p=" << *p << std::endl;      // 5,               value of x (p dereferenced)
    std::cout << "&p=" << &p << std::endl;      // 0x7fff6447a820,  address of p pointer

    // change value of x thru p
    // p = 6;                                   // error,           can't set int* to int
    *p = 6;                                     
    std::cout << "x=" << x << std::endl;        // 6


    int y = 2;
    // int *q = y;                              // error can't initiate with type int, needs int*


    // pointer to a function
    int (*fp)(int);
    std::cout << "&fp=" << &fp << std::endl;        // 0x7fff66da6810,  address of pointer fp
    std::cout << "fp=" << fp << std::endl;          // 0,               value of pointer fp

    fp = &next;                                     // fp points to function next(int)
    fp = next;                                      
    std::cout << "&next=" << &next << std::endl;    // 1,               address of function?
    std::cout << "fp=" << fp << std::endl;          // 1,               value is address of function?
    std::cout << "&fp=" << &fp << std::endl;        // 0x7fff66da6810,  address of pointer fp?
    std::cout << "*fp=" << *fp << std::endl;        // 1,               address of function?

    // calling function thru pointer
    int i = 0;
    i = (*fp)(i);
    std::cout << "i=" << i << std::endl;            // 99
    i = fp(i);
    std::cout << "i=" << i << std::endl;            // 198



    // function returns pointer to function
    fptr fp_ptr = return_func_ptr();
    std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl;      // 1
    std::cout << "fp_ptr=" << *fp_ptr << std::endl;                         // 1
    std::cout << "&fp_ptr=" << *fp_ptr << std::endl;                        // 1
    std::cout << "*fp_ptr=" << *fp_ptr << std::endl;                        // 1

    int j = fp_ptr(1);                              
    std::cout << "j=" << j << std::endl;                                    // 100

}
4

2 に答える 2

3
std::cout << "fp_ptr=" << *fp_ptr << std::endl;

する必要があります

std::cout << "fp_ptr=" << (void*)fp_ptr << std::endl;

演算子には関数ポインターのcoutオーバーロードがないため、bool代わりに使用します。そのため、出力として常に 1 が得られます。コードをコンパイルすると、常に true と評価されるという警告が表示されます。すべての警告をオンにして、それらを取り除くようにしてください。

于 2013-07-07T08:20:28.483 に答える