你/可以/你从一个函数返回什么......并期望它在到达时还活着? [英] what /can/ you return from a function .. and expect it to be alive at arrival?

查看:47
本文介绍了你/可以/你从一个函数返回什么......并期望它在到达时还活着?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再一次因为期望一个函数返回一个正确的值而感到失望,然后我感到失望..反而出现了奇怪的行为和误导性的调试信息.众所周知,您不能从函数返回局部变量并期望它按预期到达.测试

Once again I got cought on expecting a function to return a proper value and then be disapointed .. getting odd behavior and misleading debug-information instead. It's fairly well known, that you cannot return a local variable from a function and expect it to arrive as you would expect. Testing

int i=2;
int k=4;
return make_pair<int,int>(i*i,k*k);

确实返回了一些值得尊敬的东西.但是使用比简单类型更复杂的对象似乎每次都会吸引我.那么,是否有任何形式可以用来区分哪些可以和哪些不能从函数安全返回?----------- 在编辑时添加:------------这是一个行不通的例子,它被粗暴地断章取义了.问题上下文是屏幕矩形的(将成为 GUI)树.类节点继承自包含 3 个指向普通类型的指针(再次用于使值保持不变)的基(矩形).. 基在构造函数中使用 new

Does indeed return something respectable. But using more elaborate objects than simple types seems to catch me every time. So, is there any formality that I can use for discriminating on what can and what cannot be returned safely from a function? ----------- added on edit: ------------ Here is the example that does not work, taken brutally out of context. Problem-context is a (to be GUI) tree of rectangles for the screen. Class node inherits from a base (rectangle) containing 3 pointers to plain types (again, used to make values stick) .. the base uses new in constructor

pair<node,node> node_handler::split( vector<node>::iterator& this_node, double ratio, bool as_horizontal ){
//this_node becomes parents to the split-twins
    this_node->my_ratio=ratio;
    double firstW, firstH;
    double secW, secH;
    glm::dvec2 afirst, asecond;
    if(as_horizontal ){
        firstW = *this_node->plWidth*LETTER_PIXEL_WIDTH;
        firstH = *this_node->plHeight*LINE_PIXEL_HEIGHT*ratio;
        afirst = *this_node->pPoint;
        secW   = firstW;
        secH   = LINE_PIXEL_HEIGHT*(*this_node->plHeight)*(1.0d-ratio);
        asecond= afirst+glm::dvec2(0.0d, firstH);
    }
    else{
        firstW = ratio*(*this_node->plWidth)*LETTER_PIXEL_WIDTH;
        firstH = *this_node->plHeight*LINE_PIXEL_HEIGHT;
        afirst = *this_node->pPoint;
        secW   = (1.0d*ratio)*(*this_node->plWidth)*LETTER_PIXEL_WIDTH;
        secH   = firstH;
        asecond= afirst+glm::dvec2(firstW,0.0d);
    }
    return make_pair<node,node>( node(afirst ,firstW, firstH) , node(asecond ,secW, secH)  ) ;
}

推荐答案

从技术上讲,您可以从函数返回任何内容.

Technically, you can return anything from a function.

现在,当你返回一个指针或对局部事物的引用时,就会出现问题.

Now when you return a pointer or a reference to something that is only local, then you have a problem.

解决方案:

  1. 返回副本(无论如何都可以省略副本)
  2. 返回 shared_ptr<>/unique-ptr<> 以获取不能复制的内容.
  3. 仅返回基本类型并将对可能被修改的对象的引用传递给函数.
  1. Return copies (OK with copy elision anyway)
  2. Return shared_ptr<>/unique-ptr<> for something that must not be copied.
  3. Return only basic types and pass to the function a reference to an object that might be modified.

不要在函数中创建需要手动销毁层的东西(比如用new创建的指针).

Do not create something in the function that needs to be manually destroyed layer (say, a pointer created with new).

这篇关于你/可以/你从一个函数返回什么......并期望它在到达时还活着?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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