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

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

问题描述

我来自C#的C#背景。假设我有一个方法在堆栈中的方法中创建一个对象,然后将它传递给另一个类方法,将其添加到一个memeber向量。

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

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

这里是我的问题:


  1. 一旦DoStuff方法结束,将超出范围并从堆栈中弹出?

  2. memVec

  3. 将堆栈对象传递给存储为指针的方法的正确方法是什么?

  4. ol>

    我意识到这些对于一些C ++程序员来说可能是明显的。



    标记

    解决方案


    1. 是的。

    2. 指针保持活着 - 长居民对象。这意味着,当你第一次尝试取消引用这样的指针,你会进入未定义的行为(可能你的程序会崩溃,或者更糟的是,将继续运行给出奇怪的结果)。

    3. 如果你想在函数返回后保留它们,你根本不这样做。这就是为什么要使用堆分配和存储对象的副本的容器。

    最简单的方法来实现你试图做的是在正常的STL容器(例如 std :: vector )中存储对象的副本。如果这样的对象是重量级的和昂贵的复制,你可能想分配他们在堆上存储他们在一个足够的智能指针的容器,例如。 boost :: shared_ptr (请参阅 @ Space_C0wb0y的回答)。

    另一种可能是使用 boost :: ptr_vector boost :: ptr_vector_owner 这最后一个类负责拥有存储在相关联的 ptr_vector 中的对象,并且当它超出范围时删除所有指针。有关 ptr_vector ptr_vector_owner 的更多信息,您可能需要查看本文


    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天全站免登陆