相关疑难解决方法(0)

为什么函数指针定义适用于任意数量的&符号'&'或星号'*'?

为什么以下工作?

void foo() {
    cout << "Foo to you too!\n";
};

int main() {
    void (*p1_foo)() = foo;
    void (*p2_foo)() = *foo;
    void (*p3_foo)() = &foo;
    void (*p4_foo)() = *&foo;
    void (*p5_foo)() = &*foo;
    void (*p6_foo)() = **foo;
    void (*p7_foo)() = **********************foo;

    (*p1_foo)();
    (*p2_foo)();
    (*p3_foo)();
    (*p4_foo)();
    (*p5_foo)();
    (*p6_foo)();
    (*p7_foo)();
}
Run Code Online (Sandbox Code Playgroud)

c c++ function-pointers

204
推荐指数
2
解决办法
1万
查看次数

函数指针有什么意义?

我很难看到函数指针的实用程序.我想它在某些情况下可能是有用的(毕竟它们存在),但我想不出使用函数指针更好或不可避免的情况.

你能举出一些好用函数指针的例子(在C或C++中)吗?

c c++ pointers function c++-faq

85
推荐指数
8
解决办法
1万
查看次数

为什么`(*)`可以从函数参数列表中的函数指针中省略?

用gcc8编译:

#include <stdio.h>
void some_func(void f1(void), void (*f2)(void))
{
    printf("%d\n", f1);
    printf("%d\n", f2);
}
Run Code Online (Sandbox Code Playgroud)

给(仅)以下警告:

<source>:11:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'void (*)(void)' [-Wformat=]
     printf("%d\n", f1);
<source>:12:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'void (*)(void)' [-Wformat=]
     printf("%d\n", f2);
Run Code Online (Sandbox Code Playgroud)

为什么类型f1相同f2?仅f2被声明为函数指针.我希望f1不会编译,因为它命名一个函数类型,而不是函数指针.什么规则说,函数参数列表中的函数类型更改为指向该函数类型的指针?

c

15
推荐指数
1
解决办法
593
查看次数

功能指针 - 2个选项

我想知道这两个函数之间有什么区别(funfun2)我知道这fun2是函数指针但是有什么用fun?这是相同的,因为还有通过指针的功能名称吗?

#include <iostream>

void print()
{
  std::cout << "print()" << std::endl;
}

void fun(void cast())
{
  cast();
}

void fun2(void(*cast)())
{
  cast();
}

int main(){
  fun(print);
  fun2(print);
} 
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers

8
推荐指数
1
解决办法
129
查看次数

if语句中的函数名以奇怪的方式转换

使用此代码(有效的C++ 11):

#include <stdio.h>
#include <typeinfo>

bool my_awesome_func(int param) {
  return (param > 1);
}

int main(int argc, char const *argv[]) {
  fprintf(stderr, "type of my_awesome_func: %s\n", 
          typeid(my_awesome_func).name());
  if (my_awesome_func) {
    fprintf(stderr, "WHAT???\n");
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题出在if声明中.虽然typeid返回一些看起来像的东西FbiE(我认为是函数类型的gcc语言)我不明白为什么这种类型被隐式转换成bool(只是一个例子,也适用int).

为什么if语句编译和评估为真?

c++ types casting function

6
推荐指数
1
解决办法
94
查看次数

为什么只有函数指针而不是函数变量?

在C/C++中,我们可以声明/定义一个类型的函数指针,然后声明/定义这个类型的一些变量。但我认为这是模棱两可的。

例如:

typedef void ( *pFunc )();
// typedef void ( aFunc )();
void theFunc() {
   cout << "theFunc has been called successfully." << endl;
};

int main() {
   pFunc pf0 = theFunc;
   pFunc pf1 = &theFunc;

   pf0();
   ( *pf0 )();
   pf1();
   ( *pf1 )();
};
Run Code Online (Sandbox Code Playgroud)

理论上,只有pFunc pf1 = &theFunc;(*pf1)();是合法的,但以上都可以通过编译。

Pascal语法中,我们需要分别定义函数的vars或函数指针的vars,它们的含义是不同的,而且更加清晰(至少我是这么认为的)!

此外,我们不能声明/定义函数的变量而不是函数指针的变量!我尝试了以下并失败了。

typedef void ( aFunc )();
aFunc af0 = theFunc;
Run Code Online (Sandbox Code Playgroud)

如果使用其他类型,例如 int/double,则有非常严格的语法限制我们正确使用它们。(如果int*与 不同int,为什么*pf0与 相同pf0?!) …

c++ typedef function-pointers function

5
推荐指数
1
解决办法
191
查看次数

通过指针和名称将函数传递给另一个函数

我正在学习函数指针和wiki的这个例子:

int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}

int main()
{
    int  a, b;
    int  (*plus)(int, int) = add;
    a = operation(7, 5, plus);
    b = operation(20, a, subtract);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如我所看到的,有plus一个指向函数的指针 …

c++ pointers function

4
推荐指数
1
解决办法
211
查看次数

标签 统计

c++ ×6

function ×4

c ×3

function-pointers ×3

pointers ×2

c++-faq ×1

casting ×1

typedef ×1

types ×1