简体   繁体   English

如果不满足条件,我应该在成员函数中返回什么?

[英]What should I return in a member function if a condition is not met?

For example: 例如:

class Foo{
   int data;
   // ...
}

And there is a member function: 并且有一个成员函数:

Foo func(int a){
  if(a > 10) return Foo(a);
  else {} // problem is here, I do not want return 
             any Foo object, do I throw exception? how?
}

So, I do not want to return a new empty Foo object in else block, how can I handle this situation in an idiomatic way? 因此,我不想在else块中返回一个新的空Foo对象,如何以惯用的方式处理这种情况?

Yes, if you've found that there's some scenario in which your function should suddenly not be returning a value, then either: 是的,如果您发现在某些情况下您的函数应该突然不返回值,则可以:

  • throw an exception, or 引发异常,或者
  • your design is broken 您的设计已损坏

If the pre-condition is absolutely needed for the function to continue, then throwing an exception is the proper way of handling it. 如果继续运行该函数绝对需要先决条件,则抛出异常是处理它的正确方法。 If the condition isn't required explicitly throughout the rest of the function, then you have a couple options: 如果在其余函数中没有明确要求该条件,则有两种选择:

  • Return a unique pointer that manages a pointer, ie std::unique_ptr . 返回管理指针的唯一指针,即std::unique_ptr I don't recommend this approach because it has unnecessary dynamic memory allocation. 我不推荐这种方法,因为它具有不必要的动态内存分配。
  • Return boost::optional<Foo> or in a future TS std::experimental::optional<Foo> . 返回boost::optional<Foo>或将来的TS std::experimental::optional<Foo> This expresses intent that the value may or may not be there. 这表示该值可能存在或可能不存在。
  • Re-evaluate your design. 重新评估您的设计。 Why does the return depend on the condition? 为什么退货取决于条件? What could be done differently? 有什么不同的方法可以做? etc. 等等

your best bet is to return a pointer: 最好的选择是返回一个指针:

Foo* func(int a){
  if(a > 10) return new Foo(a);
  else return 0; // else return plain zerro
}

C++11 C ++ 11

Foo* func(int a){
  if(a > 10) return new Foo(a);
  else return nullptr;
}

of course you'll need to check if returned object does exist in your calling function: 当然,您需要检查调用的函数中是否确实存在返回的对象:

Foo* ptr = func(5);
if(ptr)
{
    // use ptr
    *ptr;
}
else
{
    // do not use it
}

// and delete it
if(ptr) delete ptr;

// in c++11 version no need to check just:
delete ptr;

sorry I didn't see you do not want to return 0... 抱歉,我没有看到您不想返回0 ...

Foo* func(int a){
  if(a > 10) return new Foo(a);
  else throw(a);
}

try
{
    Foo* get = func(4); // this will throw
}

catch(int)
{
     //handle the error here
}

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

相关问题 我应该从静态成员方法返回什么类型的指针 - What type of pointer should I return from static member method 我应该在简单的访问成员函数中使用const返回类型吗? - Should I use const return type in a simple access member function? 我的职能应该是Qt的成员吗? - What should my function be a member of in Qt? 函数返回一个指向对象的指针,在这种情况下我该返回什么? - function returns a pointer to an object, what should I return in this case? 如果我的 function 使用 static 成员,我为什么要通过引用返回 - why should I pass by reference return if my function use static member 我应该在构造函数中调用成员函数 - Should I call a member function in a constructor 即使成员函数修改了指针指向的内容,我也应该将其声明为const吗? - Should I declare a member function as `const` even if it modifies what a pointer points to? 如何在满足条件时停止折叠表达式 function 在中间调用并返回该值? - How to stop fold expression function calls in the middle when a condition is met and return that value? 在错误情况下应该返回模板的函数应该返回什么 <T> 值? - What should I return in an error case in a function expected to return a Template<T> value? 查找功能应该在失败结果中返回什么? - What should a find function return in failure result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM