我对C++中函数的返回类型有疑问。
为什么?
return pair<int, int>(1, 1);
和
return vector<int>(3, 3);
工作我不应该事先创建一个本地实例var并将其作为返回值吗,就像返回a一样,因为
return int 1;
不起作用。
// ...
int fun1() {
// return int 1;
int a = 1;
return a;
}
pair<int, int> fun2() {
return pair<int, int>(1, 1);
}
vector<int> fun3() {
return vector<int>(3, 3);
}
int main(){
cout << fun1() << endl;
cout << fun2().first << endl;
cout << fun3()[1] << endl;
return 0;
}
返回的样式是否只能应用于具有特定构造函数的类实例?下面的示例可以起作用。
我正在寻找确认或参考资料。
class A {
public:
int a;
A(int a_) : a(a_) {};
};
A fun4() {
return A(1);
}
代码样本测试位置:
Apple LLVM 6.0版(clang-600.0.56)(基于LLVM 3.5svn)
目标:x86_64-apple-darwin14.0.0
螺纹型号:posix