gpt4 book ai didi

C++:在函数中使用调用者的局部变量安全吗?

转载 作者:搜寻专家 更新时间:2023-10-31 01:18:56 24 4
gpt4 key购买 nike

我认为最好使用代码示例来描述情况:

int MyFuncA()
{
MyClass someInstance;
//<Work with and fill someInstance...>

MyFuncB( &someInstance )
}

int MyFuncB( MyClass* instance )
{
//Do anything you could imagine with instance, *except*:
//* Allowing references to it or any of it's data members to escape this function
//* Freeing anything the class will free in it's destructor, including itself

instance->DoThis();
instance->ModifyThat();
}

下面是我直截了当的问题:

  • C 和 C++ 标准是否保证上述概念按预期工作?为什么(不)?
  • 谨慎谨慎地执行此操作是否被视为不良做法?

最佳答案

Is the above concept guranteed, by C and C++ standards, to work as expected? Why (not)?

是的,它会按预期工作。 someInstanceMyFuncA 范围内可用。对 MyFuncB 的调用在该范围内。

Is this considered doing this, sparingly and with care, bad practice?

不明白为什么。

关于C++:在函数中使用调用者的局部变量安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6658489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com