为什么一个函数声明在另一个函数中编译,它做什么
Why does a function declaration within another function compile and what does it do?
我打算调用私有类成员函数,但由于copy&paste错误,粘贴了头文件中声明该函数的行:
void DebugView::on_cbYAxisEnabled_stateChanged(int)
{
void updateAxisEnabled();
}
不是void DebugView::on_cbYAxisEnabled_stateChanged(int)
{
updateAxisEnabled();
}
令人惊讶的是,代码被编译并执行。但是方法updateAxisEnabled()
没有被执行。
那么,为什么它可以编译呢?这里是一个局部函数声明在一个方法体或有void
指示编译器忽略之后?
编译器为Visual Studio 2008。
注::我知道函数中的类声明/定义,但不知道c++中函数中的函数。
void updateAxisEnabled();
是一个函数声明。
#include <cstdio>
void a();
void b();
int main(void) {
a();
b();
return 0;
}
void a() {
void c(); // Declaration
c(); // Call it
}
void b() {
c(); // Error: not declared
}
void c() {
puts("Hello, world!");
}
完全允许在函数作用域中声明函数:函数可以在任何作用域中声明。
c++程序员的一个常见错误确实是:void foo()
{
MyObject bar(); // 1
bar.someMethod(); // 2
}
这将悲惨地编译失败,因为第1行是而不是声明名为bar
的MyObject
并显式调用其构造函数;相反,它声明了一个名为bar
的函数,返回一个MyObject
。因此,确实没有对象可以调用someMethod
。
相关文章:
- 跨模板化函数编译的静态变量
- 无法使用 LoadObject() 函数编译 UE4 Actor。
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 函数编译,即使它不接受整数
- 使用C 模板函数编译时间递归
- 安卓 JNI - 'raw'函数编译失败
- C++就地析构函数编译警告
- 对 TR1 使用 boost 时提升数学特殊函数编译错误
- 显式复制构造函数编译错误
- 使用SSE内部函数编译一个简单的c++程序
- 函数编译时错误
- 模板函数编译错误
- C++:使用类型名作为基的模板类调用函数编译时错误
- 将 MATLAB 函数编译成可以在 linux 终端上运行的东西,w.out MATLAB
- boost::绑定不要使用成员模板函数编译
- C++函数编译错误
- 加速模板函数编译
- 如何在C++中使用辅助函数编译以下flex文件
- 如何让g++使用move构造函数编译c++11代码
- 同时将一组函数编译为.LIB和.DLL