我有以下代码.
#include <iostream>
int * foo()
{
int a = 5;
return &a;
}
int main()
{
int* p = foo();
std::cout << *p;
*p = 8;
std::cout << *p;
}
Run Code Online (Sandbox Code Playgroud)
而代码只是运行而没有运行时异常!
输出是 58
怎么会这样?本地变量的内存不能在其功能之外无法访问吗?
struct Foo {
int i;
public ref int I => ref i;
}
Run Code Online (Sandbox Code Playgroud)
此代码引发编译错误CS8170,但如果Foo是类,则不会.为什么结构不能将成员作为引用返回?