c++ - 将函数中创建的对象分配给成员变量

标签 c++ object scope member

所以我对 C++ 以及随之而来的所有内存管理还相当陌生。

我的问题是我在函数 expand 中创建一个对象,然后将所述对象分配给同一类的另一个对象中的成员变量(该类是一个二叉树,其数据是InVec 对象)。

函数运行后,给定SPnodem_leftm_right的内存位置是相同的,但是取消引用的值是垃圾.

我几乎可以肯定这是由于 InVecSPnode 对象超出范围并被销毁,从而产生指针 m_left> 和 m_right 指向垃圾邮件。

我的问题是:如何在函数内创建一个对象,将其分配给成员变量,并且在函数终止时不将其销毁?我确信我错过了一些明显的东西,但我无法在任何地方找到答案(我什至不知道如何表达它)。

这是我的代码:

void expand(SPnode &ASP)
{
    if (!(ASP.isLeaf())) return;

    int axis(ASP.m_box.getWidthAxis());
    Interval width(ASP.m_box.getWidth());

    InVec lowerInVec(lower(ASP.m_box, width, axis));
    InVec upperInVec(upper(ASP.m_box, width, axis));


    SPnode leftChild(lowerInVec);
    std::cout << &leftChild << "\n";

    SPnode rightChild(upperInVec);
    std::cout << &rightChild << "\n";


    ASP.m_left = &leftChild;
    std::cout << (ASP.m_left) << "\n";

    ASP.m_right = &rightChild;
    std::cout << (ASP.m_right) << "\n";
}

如果这段代码的形式不好或者我违反了一些重要的规则,我深表歉意 - 任何建设性的批评也将不胜感激。

编辑:以下是 Interval、InVec 和 SPnode 的相关代码:

// The Interval class is the base object on which all other classes in this
// module are built. Its implementation is intuitive, as all mathematical
// operations that are relevant for nonlinear image computation are given
// as overloaded operators (i.e. intervalA + intervalB returns the expected
// result from basic interval arithmetic).
class Interval
{
private:

    // infimum (lower bound) of interval
    double m_inf;

    //supremum (upper bound) of interval
    double m_sup;

public:
    Interval(double inf, double sup): m_inf(inf), m_sup(sup) {}

    // getter member functions, where getLen returns the length of the interval
    // and getMidpt returns the midpoint
    double getInf() const {return m_inf;}
    double getSup() const {return m_sup;}
    double getLen() const {return m_sup - m_inf;}
    double getMidpt() const {return (m_inf + m_sup)/2.0;}


// --- Headers -----------------------------------------------------------------

    // determines if a double is in the interval
    bool containsVal(double val) const;

    // determines if another interval is contained in the interval
    bool contains(const Interval &other) const;

    // performs scalar multiplication on the interval
    Interval scalarMul(double scal) const;


    // operator overloading - the specifics of interval arithmetic can be found
    // in a book or online
    friend Interval operator+(const Interval &intA, const Interval &intB);
    friend Interval operator-(const Interval &intA, const Interval &intB);
    friend Interval operator*(const Interval &intA, const Interval &intB);
    friend bool operator==(const Interval &intA, const Interval &intB);
    friend bool operator!=(const Interval &intA, const Interval &intB);
    friend std::ostream& operator<< (std::ostream &out, const Interval &intA);


    friend void expand(SPnode &ASP);
    friend InVec lower(const InVec &box, const Interval &width, int axis);
    friend InVec upper(const InVec &box, const Interval &width, int axis);
};

class InVec
{
private:

    // this is a vector containing the Interval objects that make up the InVec 
    // object
    std::vector<Interval> m_intervals;

public:

    InVec(std::vector<Interval> intervals): m_intervals(intervals) {}

    // returns m_intervals
    std::vector<Interval> getIntervals() const {return m_intervals;}

    // returns the interval at given axis (i.e. index) in m_intervals
    Interval getInterval(int axis) const {return m_intervals.at(axis);}

    // sets the interval at given axis to given Interval object
    void setInterval(int axis, const Interval &intA)
    {m_intervals.at(axis) = intA;}


// --- Headers -----------------------------------------------------------------

    // determines if another InVec object is contained in this InVec object
    bool contains(const InVec &IVB) const;

    // returns the length of the largest Interval object in m_intervals - note
    // that it is necessary to compute this first, before the actual largest
    // Interval can be determined
    double getWidthSize() const;

    // returns the Interval in m_intervals with the longest length 
    // (i.e. the width)
    Interval getWidth() const;


    // returns the axis (i.e. index) on which the width occurs
    double getWidthAxis() const;


    // operator overloading
    friend InVec operator+(const InVec &IVA, const InVec &IVB);
    friend InVec operator-(const InVec &IVA, const InVec &IVB);
    friend InVec operator*(const InVec &IVA, const InVec &IVB);
    friend bool operator==(const InVec &intA, const InVec &intB);
    friend bool operator!=(const InVec &intA, const InVec &intB);
    friend std::ostream& operator<< (std::ostream &out, const InVec &IVA);


    friend void expand(SPnode &ASP);
    friend InVec lower(const InVec &box, const Interval &width, int axis);
    friend InVec upper(const InVec &box, const Interval &width, int axis);

};

class SPnode
{
private:

    InVec m_box;

    // left and right children of this SPnode object - note that these must be
    // pointers in order to use them in the definition of the class, otherwise 
    // SPnode would have an inifinite definition
    SPnode* m_left;
    SPnode* m_right;

public:

    SPnode(InVec box): m_box(box), m_left(NULL), m_right(NULL) {}


    // getters and setters
    InVec getBox() const {return m_box;}
    SPnode* getLeft() const {return m_left;}
    SPnode* getRight() const {return m_right;}
    void setBox(const InVec box) {m_box = box;}
    void setLeft(SPnode* const p_node) {m_left = p_node;}
    void setRight(SPnode* const p_node) {m_right = p_node;}

    bool isLeaf() const;

    friend std::ostream& operator<< (std::ostream &out, const SPnode &ASP);
    friend void expand(SPnode &ASP);
    friend InVec lower(const InVec &box, const Interval &width, int axis);
    friend InVec upper(const InVec &box, const Interval &width, int axis);  
};

最佳答案

您必须动态分配节点。而且您需要使用智能指针来管理该内存 - 最有可能的是 std::unique_ptr

您的代码将类似于:

ASP.m_left = std::make_unique<SPnode>(lowerInVec);

这表明 SPNode 构造函数不会尝试保留对其参数的引用(或地址)。如果是这样,则所述参数也必须动态分配。

关于c++ - 将函数中创建的对象分配给成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625800/

相关文章:

javascript - Javascript 匿名函数内部的作用域

c++ - 我的overflow()的实现有什么问题?

javascript - 1.constructor 和 (1).constructor 的区别

java - c++ "new"关键字与 java 的比较

java - 对象比较

javascript - 闭包 - JS Fiddle 和简单 HTML 文件上的不同行为

angularjs - 为什么模型不会在这个递归 Angular 指令中更新?

c++ - 减少 if/if 代码量 (C++)

c++ - 为什么此代码在 Windows 7 Beta 上丢失句柄?

c++ - 如何创建共享概念的对象 vector ?