为什么当我使用没有括号的函数时,C ++编译器不提示? [英] Why doesn't the C++ compiler complain when I use functions without parentheses?

查看:295
本文介绍了为什么当我使用没有括号的函数时,C ++编译器不提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看一个朋友给我的一些代码,他说:它编译,但不工作。我看到他使用没有括号的函数,类似这样:

I was looking at some code a friend sent me, and he said: "It compiles, but doesn't work". I saw that he used the functions without the parentheses, something like this:

void foo(){
  cout<< "Hello world\n";
}

int main(){
  foo; //function without parentheses
  return 0;
}

我说的第一个是使用括号,你必须。然后我测试那个代码 - 它编译,但是当执行时不工作(没有显示Hello world)。

The first I said was "use parentheses, you have to". And then I tested that code - it does compile, but when executed doesn't work (no "Hello world" shown).

警告所有从编译器 GCC 4.7 ),但不工作?

So, why does it compile (no warning at all from the compiler GCC 4.7), but doesn't work?

推荐答案

警告您是否将警告级别设置为足够高。

It surely warns if you set the warning level high enough.

函数名称计算函数的地址,是一个法律表达式。通常它保存在函数指针中。

A function name evaluates to the address of the function, and is a legal expression. Usually it is saved in a function pointer,

void (*fptr)() = foo;

但不是必需的。

这篇关于为什么当我使用没有括号的函数时,C ++编译器不提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆