简体   繁体   English

函数返回一个指向对象的指针,在这种情况下我该返回什么?

[英]function returns a pointer to an object, what should I return in this case?

If you have a function that returns a pointer to an object by looking for an object with a specific attribute in an array, what should I return if I don't find a corresponding object in the array? 如果你有一个通过查找数组中具有特定属性的对象返回指向对象的指针的函数,如果我在数组中找不到相应的对象,应该返回什么? And, if done badly, could this represent a risk to the security or stability of the program? 而且,如果做得不好,这是否会对计划的安全性或稳定性造成风险?

If you have a function that returns a pointer to an object by looking for an specific attribute into an array of those objects, what should i return if i don't find that attribute in the array? 如果你有一个通过在这些对象的数组中查找特定属性来返回指向对象的指针的函数,那么如果我在数组中没有找到该属性,我应该返回什么? and if done badly could this represent a risk to the security or stability of the program? 如果做得不好,这是否会对计划的安全性或稳定性造成风险?

You have three basic possibilities: 您有三种基本可能性:

  • Simply return a null pointer. 只需返回一个空指针。 This is the easiest way and very probably the best. 这是最简单的方法,也可能是最好的方式。
  • Throw an exception. 抛出一个例外。 Preferably of a special type, but std::out_of_range might do too. 最好是特殊类型,但std::out_of_range也可以。
  • Return a pointer to a default object. 返回指向默认对象的指针。 Only reasonable if the return value must to point to a valid object. 只有在返回值必须指向有效对象时才合理。

Whatever you choose, it must be documented and as consistent to other cases as possible. 无论您选择什么,都必须记录在案,并尽可能与其他案例保持一致。

You could return nullptr with the convention that the caller should test that case. 您可以使用调用者应测试该情况的约定返回nullptr Or you could throw an exception. 或者你可以抛出异常。

You might use some smart pointers. 你可能会使用一些智能指针。

With C++11 you could take a different approach: passing a lambda function to deal with the found object. 使用C ++ 11,您可以采用不同的方法:传递lambda函数来处理找到的对象。

If you have a function that returns a pointer to an object by looking for an specific attribute into an array of those objects, what should i return if i don't find that attribute in the array? 如果你有一个通过在这些对象的数组中查找特定属性来返回指向对象的指针的函数,那么如果我在数组中没有找到该属性,我应该返回什么? and if done badly could this represent a risk to the security or stability of the program? 如果做得不好,这是否会对计划的安全性或稳定性造成风险?

As Columbo said: 正如科伦博所说:

You have three basic possibilities: 您有三种基本可能性:

  • Simply return a null pointer. 只需返回一个空指针。 This is the easiest way and very probably the best. 这是最简单的方法,也可能是最好的方式。
  • Throw an exception. 抛出一个例外。 Preferably of a special type, but std::out_of_range might do too. 最好是特殊类型,但std::out_of_range也可以。
  • Return a pointer to a default object. 返回指向默认对象的指针。 Only reasonable if the return value must to point to a valid object. 只有在返回值必须指向有效对象时才合理。

However, I disagree on your choice of options. 但是,我不同意你选择的选项。 Only the first two can reasonably be considered, unless you are certain you will always return a valid object. 除非您确定始终返回有效对象,否则只能合理地考虑前两个。

And, if done badly, could this represent a risk to the security or stability of the program? 而且,如果做得不好,这是否会对计划的安全性或稳定性造成风险?

Yes. 是。 Imagine you select possibility #3 and document it in your API. 想象一下,您选择了可能性#3并将其记录在您的API中。 The caller is expecting a valid object each time. 调用者每次都期待一个有效的对象。 And let's say your app is a critical component of a server and an attacker finds an exploit that results in overwriting data in the table. 并且假设您的应用程序是服务器的关键组件,并且攻击者发现了一个漏洞,导致覆盖表中的数据。 This will most probably lead to a crash of your app: you get instant denial of service attack. 这很可能会导致您的应用崩溃:您立即遭受拒绝服务攻击。 And this really isn't a far fetched scenario… 这真的不是一个遥不可及的场景......

Without even getting that far, if done badly, you could return invalid pointers, which may lead to app crash. 甚至没有那么远,如果做得不好,你可能会返回无效指针,这可能会导致应用程序崩溃。 Then again, anything done badly leads to the dark side... 然后,任何做得很糟的事都会导致黑暗的一面......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种情况下应该使用什么智能指针? - What smart pointer should I use in this case? 在错误情况下应该返回模板的函数应该返回什么 <T> 值? - What should I return in an error case in a function expected to return a Template<T> value? 如果未分配返回未定义对象类型引用的C ++函数的返回值,会发生什么? - What should happen when the return value from a C++ function that returns a reference of an undefined object type is not assigned? 这个函数应该返回什么类型的对象? - What type of object should this function return? 在这种情况下应该删除指针吗? - Should I delete the pointer in this case? 我应该如何定义一个返回对指针的引用的函数? (引用指针与指向指针的指针) - How should I define a function that return reference to a pointer? (reference to pointer vs pointer to pointer) 我应该从静态成员方法返回什么类型的指针 - What type of pointer should I return from static member method 我应该为指针分配使用哪种返回类型? - What return type should I use for a pointer assignment? 我该如何从函数中返回一个对象? - How should I return an object from a function? 我应该使用什么函数签名来返回对可能不存在的对象的引用? - What function signature should I use to return a reference to an object that might not exist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM