函数参数寿命 [英] Function argument lifespan

查看:64
本文介绍了函数参数寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,


如下面的简单示例程序所示,函数参数在评估函数的返回值后被销毁。因为

与本地函数变量相反,后者在

函数返回之前被销毁。


这是正确的吗? (据我所知,这是)

标准中描述的是什么?


谢谢,

Andre


---示例---


#include< iostream>


使用命名空间std;


班级考试

{

public:

test(string s =" ")

:s_(s)

{

cout<< "测试::测试()\t" << s_<<结束;

}


test(const test& org)

{

cout< ;< "测试测试::(复印件)" << endl;

s_ ="副本 + org.s_;

}


~test()

{

cout< < "测试::〜测试()\t" << s_<< endl;

}


const string& getStr()const

{

返回s_;

};


私人:

string s_;

};


const test& f(测试arg)

{

test l(local);

return arg;

}


int main()

{

test c(" main");

cout<< \ n \\\t\tf()= [" << f(c).getStr()<< "] \\\
" << endl;

}

解决方案

在***** @ gmail.com 写道:

正如下面的简单示例程序所示,函数参数
在返回值后被销毁该功能已经过评估。与本地函数变量相反,它们在函数返回之前被销毁。

这是正确的吗? (据我所知)
标准中描述的是什么?




3.8对象生命周期,12.2临时对象。可能更多...


V




Victor Bazarov写道:

标准中描述的是什么?



3.8对象生命周期,12.2临时对象。可能更多......

V




Victor,


非常感谢你的支持快速且知识渊博的答案!

非常感谢!


谢谢,

Andre



in*****@gmail.com 写道:

全部,

正如下面的简单示例程序所示,在评估函数的返回值之后,函数参数被销毁。与
函数返回之前销毁的局部函数变量相反。

这是正确的吗? (据我所知,它是这样的)
标准中描述的是什么?

谢谢,
Andre

---示例 - -

#include< iostream>

使用命名空间std;

课堂测试
公开:<测试(字符串s ="")
:s_(s)
{
cout<< "测试::测试()\t" << s_<<测试(const test& org)
{
cout<< "测试测试::(复印件)" << endl;
s_ ="副本 + org.s_;
}
~test()
{
cout<< "测试::〜测试()\t" << s_<< endl;
}

const string& getStr()const
{
返回s_;
};

私人:
字符串s_;
};

const test& f(test arg)
{
test l(local);
return arg;
}


f( )例程返回对函数参数的引用,该参数的

生命周期在函数出口处结束。因此,我认为你的程序的行为是未定义的。

int main()
{
test c(" main");
cout<< \ n \\\t\tf()= [" << f(c).getStr()<< "] \\\
" <<结束;
}




我不相信它可以从

中得出任何合理的结论程序由于上面的f()的实现。


格雷格


All,

As the simple sample program below demonstrates, function arguments are
destroyed after the return value of the function has been evaluated. As
opposed to local function variables, which are destroyed before the
function returns.

Is this correct? (To the best of my knowledge it is)
Where in the standard is this described?

Thanks,
Andre

--- Example ---

#include <iostream>

using namespace std;

class test
{
public:
test( string s = "")
: s_(s)
{
cout << "test::test()\t" << s_ << endl;
}

test( const test &org)
{
cout << "test::test(copy)" << endl;
s_ = "copy of " + org.s_ ;
}

~test()
{
cout << "test::~test()\t" << s_ << endl;
}

const string & getStr() const
{
return s_;
};

private:
string s_;
};

const test & f( test arg )
{
test l("local");
return arg;
}

int main()
{
test c("main");
cout << "\n\t\tf() = [" << f( c ).getStr() << "]\n" << endl;
}

解决方案

in*****@gmail.com wrote:

As the simple sample program below demonstrates, function arguments
are destroyed after the return value of the function has been
evaluated. As opposed to local function variables, which are
destroyed before the function returns.

Is this correct? (To the best of my knowledge it is)
Where in the standard is this described?



3.8 Object lifetime, 12.2 Temporary objects. Probably more...

V



Victor Bazarov wrote:

Where in the standard is this described?



3.8 Object lifetime, 12.2 Temporary objects. Probably more...

V



Victor,

Thank you very much for your quick and knowledgable answer!
Much appreciated!

Thanks,
Andre



in*****@gmail.com wrote:

All,

As the simple sample program below demonstrates, function arguments are
destroyed after the return value of the function has been evaluated. As
opposed to local function variables, which are destroyed before the
function returns.

Is this correct? (To the best of my knowledge it is)
Where in the standard is this described?

Thanks,
Andre

--- Example ---

#include <iostream>

using namespace std;

class test
{
public:
test( string s = "")
: s_(s)
{
cout << "test::test()\t" << s_ << endl;
}

test( const test &org)
{
cout << "test::test(copy)" << endl;
s_ = "copy of " + org.s_ ;
}

~test()
{
cout << "test::~test()\t" << s_ << endl;
}

const string & getStr() const
{
return s_;
};

private:
string s_;
};

const test & f( test arg )
{
test l("local");
return arg;
}
The f() routine returns a reference to a function parameter whose
lifetime ends at function exit. Therefore I believe that the behavior
of your program is undefined.
int main()
{
test c("main");
cout << "\n\t\tf() = [" << f( c ).getStr() << "]\n" << endl;
}



I don''t believe it''s possible to draw any sound conclusions from
running this program due to the implementation of f() above.

Greg


这篇关于函数参数寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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