编译器警告(等级 1)C4172

返回局部变量或临时变量的地址

某个函数会返回局部变量或临时对象的地址。 局部变量和临时对象在某个函数返回时会被销毁,因此返回的地址无效。

请重新设计该函数,使其不返回局部对象的地址。

以下示例生成 C4172:

// C4172.cpp
// compile with: /W1 /LD
float f = 10;

const double& bar() {
// try the following line instead
// const float& bar() {
   return f;   // C4172
}