所以:案例A.
const BoundingBox Player::GetBoundingBox(void)
{
return BoundingBox( &GetBoundingSphere() );
}
从函数GetBoundingBox()返回const BoundingBox类型的值
变体I :(将它绑定到const引用)
const BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();
变体II :(将它绑定到const副本)
const BoundingBox l_Bbox = l_pPlayer->GetBoundingBox();
两者都工作正常,我没有看到l_Bbox对象超出范围. (虽然,我在变体1中理解,复制构造函数未被调用,因此稍微好于变体II).
另外,为了比较,我做了以下更改.
案例B
BoundingBox Player::GetBoundingBox(void)
{
return BoundingBox( &GetBoundingSphere() );
}
与变体:
一世
BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();
和II:
BoundingBox l_Bbox = l_pPlayer->GetBoundingBox();
对象l_Bbox仍然没有超出范围.如何“将它绑定到调用函数中的const引用,它的生命周期将扩展到调用函数的范围”,真正将对象的生命周期延长到调用函数的范围?
我在这里错过了一些小事吗?
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/c-dangtabeibangdingdaodiaoyonghanshuzhongdeconstyinyongshi.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 25, 2019 at 09:08 下午