【问题标题】:Address of function pointers in C++ [duplicate]C ++中函数指针的地址[重复]
【发布时间】:2013-07-07 08:15:54
【问题描述】:

我不清楚调用返回的值是什么:

&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

}

【问题讨论】:

  • 您的第一个 std::cout &lt;&lt; "fp=" &lt;&lt; fp &lt;&lt; std::endl; 是未定义的行为,因为您没有初始化指针。你得到0 是偶然的。现在,如果您已将其初始化为0(在第一次使用之前将其定义为int (*fp)(int) = 0; 或分配0),那么您一定会得到0
  • 编写更具可读性的代码是一个好习惯。 C++ 允许接口。您可以获得既可读又可维护的相同结果。那么,为什么要让生活变得复杂呢?

标签: c++ function pointers


【解决方案1】:

这里有一些指针似乎不清楚:

// 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 是未定义的。这些行具有未定义的行为。

之后:

// 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

这里有两件事:

  • 在我指出的那条线上,我不确定它是否是您想要测试的。
  • 另外,cout 没有重载来获取函数指针,而是使用 bool。所以应该是:

    std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
    

我建议你阅读这篇关于函数指针的文章,它几乎解释了你需要知道的所有内容:http://www.learncpp.com/cpp-tutorial/78-function-pointers/

【讨论】:

  • 感谢您指出(不是双关语)所有这些,以及这篇文章。另外,我认为'&return_func_ptr'会显示函数的地址。
  • 指出的行应该写成 '&fp_ptr' 我还需要重新解释吗? ?
  • 不,你不应该。 void&amp; 没有任何意义...对void 的引用不正确。当你想要&amp;fn_ptr 时,你想要fn_ptr 的地址。不一样。
【解决方案2】:
std::cout << "fp_ptr=" << *fp_ptr << std::endl;

应该是

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

cout 运算符没有函数指针的重载,因此它使用 bool 代替。这就是为什么你总是得到 1 作为输出。当我编译你的代码时,我什至会收到一个警告,告诉我它总是会评估为真。您应该打开所有警告并尝试消除它们。

【讨论】:

  • fptrcast.cc:3:27: error: invalid static_cast from type ‘fptr {aka int (*)(int)}’ to type ‘void*’ static_cast&lt;void*&gt;(fp_ptr);
  • reinterpret_cast 可能会起作用。
  • 你能直接将函数指针转换为void *吗?我认为你至少需要一个reinterpret_cast
  • 是的,你是对的,我解决了这个问题。
  • @Devolus "fixed" 会更好。
猜你喜欢
  • 2019-08-19
  • 1970-01-01
  • 2019-07-26
  • 2020-12-30
  • 2021-09-20
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多