int method1() {
return 0;
}
decltype(method1) method2() {
return method1;
}
Run Code Online (Sandbox Code Playgroud)
我编译我的代码,得到一个错误:'method2'声明为函数返回一个函数,然后我将返回类型更改为函数指针,它工作,我只是想为什么它是这样.
decltype(method1) *method2() {
return method1;
}
Run Code Online (Sandbox Code Playgroud)
在C++中,无法返回函数和数组.这就是语言的设计方式.
您必须返回指针或引用它们.你已经尝试过指针,它有效.返回引用的以下内容也应该有效:
decltype(method1)& method2() {
return method1;
}
Run Code Online (Sandbox Code Playgroud)