【发布时间】:2017-06-17 18:11:42
【问题描述】:
即使以下代码编译并运行良好,我想知道它是否是有效的 c++ 代码?
int main()
{
int *i= new int;
cout<<*i;
int &ref=*i;
cout<<ref;
delete &ref; //Especially is this statement valid?
return 0;
}
如果它是有效的,那么它也必须是有效的:
int& getInt() {
int* i = new int;
return *i; // OK?
}
int main(){
int& myInt = getInt(); // these two lines are same as shown in the example above ?
delete &myInt; //is this OK too?
}
【问题讨论】:
标签: c++ reference return-by-reference