在返回语句后的函数code的执行在c ++访问 [英] Execution of code in a function after the return statement has been accessed in c++

查看:104
本文介绍了在返回语句后的函数code的执行在c ++访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

Class Image {

    IplImage* createImage( char* name )
    {
        IplImage* img = cvLoadImage( name );

        return img;

        ...
    }
}

int main() {
    IplImage* newimg = createImage( "somepath" );
    return 0;
}

的createImage 功能被执行,我想知道是否有一种方法来执行一些code return语句被访问,并在 newimg 后已经填充了 IMG的内容变量。

After the createImage function is been executed I would like to know if there's a way to execute some code after the return statement has been accessed and the newimg has been populated with the contents of the img variable.

以上code是什么,我想达到一个例子。我的主要问题是:有没有什么办法可以在return语句已经达到即使一个函数执行code

The above code is an example of what I want to achieve. My main question is: Is there any way I can execute code in a function even after the return statement has been reached?

推荐答案

NO!收益指令正是如此,它立即返回。无论是它不会被执行了。

NO! The return instruction does exactly that, it returns immediately. Whatever is after it won't be executed.

所以,如果有什么事情想这个功能做的,你必须在函数之前添加code为这些任务收益

So if there's anything else you would like this function to do, you must add the code for these tasks before the function return:

IplImage* createImage( char* name )
{
    IplImage* img = cvLoadImage( name );

    // test if image was loaded and/or
    // do whatever else you feel you need to do
    // before:

    return img;
}

记住,你的函数的createImage()应该是简单的,并执行如做广告:创建一个图像

Remember, your function createImage() should be simple and perform as advertised: create an image.

这篇关于在返回语句后的函数code的执行在c ++访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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