创建堆栈中,然后通过引用传递给另一个方法在C对对象++ [英] Creating an object on the stack then passing by reference to another method in C++

查看:126
本文介绍了创建堆栈中,然后通过引用传递给另一个方法在C对对象++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个C#背景,以C ++来了。说我有一个会在堆栈上的方法的对象的方法,那么我将它传递给另一个类方法,把它添加到一个memeber载体。

 无效DoStuff()
{
    SimpleObj所以= SimpleObj(数据,4);
    memobj.Add(左右);
}//在memobj
无效添加(SimpleObj&安培;左右)
{
   memVec.push_back(左右); //的boost :: ptr_vector对象
}

下面是我的问题:


  1. 一旦DoStuff方法两端将因此走出去的范围,从栈中弹出?

  2. memVec有一个指针左右,但它得到了在这里杀出发生了什么?

  3. 请告诉我正确的方式堆叠对象传递给方法,将它们存储作为​​指针?

我意识到这可能是显而易见的一些豁达一个C ++程序员。

标记


解决方案


  1. 指针仍然活着,而是指向一个没有再存在的对象。这意味着,首次尝试取消引用指针等,你会产生不确定的行为去(可能是你的程序会崩溃,或者更糟的是,将继续运行给奇怪的结果)。

  2. 您根本就没有做,如果你想保持他们后函数返回。这就是为什么用来存储用于堆分配和容器的拷贝的对象。

实现你正在尝试做的是存储的副本在一个正常的STL容器中的对象的的(例如的std ::矢量)。如果这样的对象是重量级的和昂贵的复制周围,你可能想分配他们在堆上它们存储在适当的智能指针,例如一个容器的boost :: shared_ptr的(见<一个例子href=\"http://stackoverflow.com/questions/3759119/creating-an-object-on-the-stack-then-passing-by-reference-to-another-method-in-c/3759171#3759171\">@Space_C0wb0y's回答)。

另一种可能性是使用关联的boost :: ptr_vector 的boost :: ptr_vector_owner ;这最后一堂课需要照顾的拥有存储在相关的 ptr_vector 的对象,并删除所有指向它超出范围时。有关更多信息, ptr_vector ptr_vector_owner ,你可能想看看<一个href=\"http://www.$c$cproject.com/KB/stl/ptr_vecto.aspx?fid=60058&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26\"相对=nofollow称号=ptr_vector - 一个容器,用于指针 - $ C $的CProject>这篇文章

I am coming from a C# background to C++. Say I have a method that creates a object in a method on the stack, then I pass it to another classes method which adds it to a memeber vector.

void DoStuff()
{
    SimpleObj so = SimpleObj("Data", 4);
    memobj.Add(so); 
}

//In memobj
void Add(SimpleObj& so)
{
   memVec.push_back(so); //boost::ptr_vector object
}

Here are my questions:

  1. Once the DoStuff methods ends will the so go out of scope and be popped from the stack?
  2. memVec has a pointer to so but it got popped what happens here?
  3. Whats the correct way to pass stack objects to methods that will store them as pointers?

I realise these are probably obvious to a C++ programmer with some expereince.

Mark

解决方案

  1. Yes.
  2. The pointer remains "alive", but points to a no-longer-existent object. This means that the first time you try to dereference such pointer you'll go in undefined behavior (likely your program will crash, or, worse, will continue to run giving "strange" results).
  3. You simply don't do that if you want to keep them after the function returned. That's why heap allocation and containers which store copies of objects are used.

The simplest way to achieve what you are trying to do would be to store a copy of the objects in a normal STL container (e.g. std::vector). If such objects are heavyweight and costly to copy around, you may want to allocate them on the heap store them in a container of adequate smart pointers, e.g. boost::shared_ptr (see the example in @Space_C0wb0y's answer).

Another possibility is to use the boost::ptr_vector in association with boost::ptr_vector_owner; this last class takes care of "owning" the objects stored in the associated ptr_vector, and deleting all the pointers when it goes out of scope. For more information on ptr_vector and ptr_vector_owner, you may want to have a look at this article.

这篇关于创建堆栈中,然后通过引用传递给另一个方法在C对对象++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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