c ++函数,在某些条件下不能返回有意义的值 [英] c++ function that can't return a meaningful value under some conditions

查看:85
本文介绍了c ++函数,在某些条件下不能返回有意义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象类型作为返回值类型的成员函数:

  MyObject myfunction(parameters){
if(some condition){
return MyObject(parameters);
} else {
...无效对象可以创建...
}
}

在某些条件下(在函数体中检查),不能创建并返回类型为MyObject的对象。



Beeing只是一个偶然的c ++程序员,我可以自发地提出三个解决方案:


  1. 将返回值类型更改为* MyObject并返回nullptr可以创建对象(C ++ 11),然后在调用代码中检查是否与nullptr相等。

  2. 如果没有对象可以被创建并且在调用代码中捕捉到对象,则抛出异常



  3. 使用一些值定义的无效对象创建一个对象,并在使用返回的对象之前检查该对象。 b $ b

    什么是处理这种情况的标准方法和性能方面的最佳解决方案? ...或一些明显的工作,我只是看不到...



    一个最先进的C ++ 11解决方案将是完美的: - )



    我的想法到目前为止:

    解决方案1似乎确定,但是只有C ++ 11,我必须创建返回的对象在堆上,以便能够将它传递给主程序(将对象本身返回到调用函数,因此保持它在堆栈可能会更快的小对象?)

    解决方案2可能会更慢,并导致主程序中的详细编码。

    解决方案3可能是最慢的(一个对象是徒劳地创建的),并不是很方便在主程序中检查。 / p>

    对于我的代码,没有有效的返回对象是默认情况比异常和创建的对象是相当小,但考虑不同情况下的一般考虑,非常感谢您的帮助: - )

    解决方案

    / div>

    所有3个建议的解决方案都是有效且常见的,具体取决于具体情况。



    如果无法创建对象,可能导致调用函数必须中止,备份和重试,或采取其他激烈的措施,然后抛出异常。



    如果无法创建对象是例程事件,并且你期望调用者检查一个对象是否被创建并在任何情况下正常进行,返回null是一个很好的解决方案。



    如果有合理的虚拟或空白对象,这是一个很好的解决方案。但这是相当罕见的。



    如果你返回一个空指针,然后你发现每一个地方你调用这个函数,你是写的

      MyObject * myobject = myfunction(whatever); 
    if(myobject == null)throw new PanicException;

    然后你可以在函数中抛出异常。



    更糟糕的是,如果你正在写:

      MyObject * myobject = myfunction 
    if(myobject!= null)
    {
    ...处理...
    }
    else
    {
    ...显示错误消息...
    }

    然后你只是模拟异常处理与IF声明。使用真正的异常。



    另一方面,如果你抛出一个异常,然后你发现你经常写:

      MyObject * myobject; 
    try
    {
    myobject = myfunction(whatever);
    }
    catch(PanicException pe)
    {
    myobject = null;
    }

    那么,你最好只是返回null。 / p>

    我偶尔会创建虚拟对象。最常见的情况是当一个函数返回一个集合,如数组或链表,如果我发现没有数据放入集合,然后返回一个零元素的集合。然后调用者循环遍历集合中的元素,如果没有元素,那就好了。我有一些情况下,我已返回一个对象的零长度字符串的名称或客户id或任何。但是一般来说,如果你只是返回一个虚拟对象,以便调用者可以测试和说,哦,它是一个虚拟对象,然后抛弃它,我想你最好返回null。



    BTW不确定你的意思,当你说,你只能在C ++ 11中返回一个空指针。传递null的能力可以追溯到我所见过的C ++的最早版本。


    I have a member function with an object type as the return value type:

    MyObject myfunction(parameters) {
        if (some condition) { 
            return MyObject(parameters);
        } else { 
            ... no valid object can be created ... 
        } 
    }
    

    Under certain conditions (checked for in the function body) an object of type MyObject cannot be created and returned.

    Beeing just an occasional c++ programmer I can spontaneously come up with three solutions:

    1. Changing the return value type to * MyObject and returning nullptr if no valid object can be created (C++11), then checking for equality to nullptr in the calling code.
    2. Throwing an exception if no object can be created and catching that one in the calling code.
    3. Creating an object with some values that I define as invalid and check for that before using the returned object.

    What would be the standard way of dealing with such a situation and the best solution in terms of performance? ... or some obvious work-around that I just don't see ...

    A state-of-the-art C++11 solution would be perfect :-)

    My thoughts so far:
    Solution 1 seems OK, but is C++11 only and I would have to create the returned object on the heap in order to be able to pass it to the main program (returning the object itself to the calling function, thus keeping it in the stack might be quicker for small objects?).
    Solution 2 might be slower and leads to verbose coding in the main program.
    Solution 3 is probably the slowest (an object is created in vain) and not very convenient to check for in the main program.

    For my code no valid return object is rather the default situation than the exception and the created object is rather small, but general considerations considering different cases are certainly useful for other readers' applications ...

    Thanks a lot to all of you for help :-)

    解决方案

    All 3 of your suggested solutions are valid and common, depending on the circumstances.

    If being unable to create the object is an error condition that is likely to cause the calling function to have to abort, back up and retry, or take other drastic measures, then throw an exception.

    If inability to create the object is a routine event, and you expect the caller to check if an object was created and proceed fairly normally in either case, returning null is a good solution.

    If there's a reasonable dummy or blank object that can be created, that's a fine solution. But this is pretty rare. You should only do this if the caller will actually process the dummy object.

    If you return a null pointer and then you find that every place you call this function you are writing

    MyObject* myobject=myfunction(whatever);
    if (myobject==null) throw new PanicException;
    

    Then you might as well just throw the exception inside the function.

    Worse, if you are writing:

    MyObject* myobject=myfunction(whatever);
    if (myobject!=null)
    {
      ... process it ...
    }
    else
    {
       ... display error message ...
    }
    

    Then you are just simulating exception handling with an IF statement. Use a real exception.

    On the other hand, if you throw an exception and then you find you are regularly writing:

    MyObject* myobject;
    try
    {
      myobject=myfunction(whatever);
    }
    catch (PanicException pe)
    {
      myobject=null;
    }
    

    Well then, you would have been better off to just return the null.

    I've occasionally created dummy objects. The most common case is when a function returns a collection, like an array or linked list, and if I find no data to put in the collection, then return a collection with zero elements. Then the caller loops through the elements in the collection, and if there are none, that's just fine. I've had a few cases where I've returned an object with a zero-length string for the name or customer id or whatever. But in general, if you're just returning a dummy object so that the caller can test and say, oh, it's a dummy object, and then throw it away, I think you're better off to return null.

    BTW not sure what you meant when you said that you could only return a null pointer in C++11. The ability to pass around nulls goes back to the earliest version of C++ that I ever saw.

    这篇关于c ++函数,在某些条件下不能返回有意义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆