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

duncster94

因此,我对C ++及其附带的所有内存管理还很陌生。

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

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

我几乎可以肯定,这是由于InVecSPnode对象超出范围并被破坏了,因此产生了指向垃圾的指针m_leftm_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";
}

如果此代码格式不正确或违反了一些重要规则,我深表歉意。任何建设性的批评也将不胜感激。

编辑:这是间隔,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);  
};
保罗·安德烈·哈克(PauloAndréHaacke)

您应该将子代创建为指针,并使用new运算符动态分配它们,如下所示:

SPnode * leftChild = new SPnode(lowerInVec);

然后将其分配给对象成员变量,如下所示:

ASP.m_left = leftChild;

同样,您应该将m_left声明为SPnode指针,如下所示:

SPNode * m_left;

对右边的人做同样的事情。

附注:在理解智能指针之前,您应该了解使用new运算符分配的指针。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果PHP总是按引用复制对象,如果将在方法中创建的对象分配给成员变量,会发生什么情况?

C ++无法将在方法中作为指针传递的对象分配给变量

在Node.js中的构造函数内部进行对象分解以将值分配给成员变量不起作用

将在构造函数中作为参数传递的函数指针分配给私有实例变量时出错

我可以将创建的对象中的数据分配给子类对象变量吗?

何时创建分配给实例变量的对象

Javascript; 通过分配给变量创建函数?

如何在Javascript的类构造函数中访问分配给对象的变量名?

如何分配给结构对象的成员?

将 Firestore JSON 对象分配给 Kotlin 中的变量

将CanvasJS中的json对象分配给javascript变量

如何将列表中的对象分配给变量?

如何将在编辑框中键入的值分配给MFC中的变量

如何将在JDBC中执行的SQL查询的结果分配给Java变量?

将函数分配给python中的变量

将函数分配给javascript中的变量?

使用从R中的向量创建的名称分配给对象

将Image对象分配给变量

Javascript:将对象分配给变量

如何删除分配给变量的对象?

将函数的结果分配给变量?

将变量分配给函数返回?

将函数分配给变量

将成员函数分配给实例成员的属性

将 lambda 函数分配给静态成员变量 (c++)

在C ++中将通用构造函数分配给成员变量时,std :: move或std :: forward

如何在TypeScript中将具有成员的函数分配给变量?

PHP-您可以将成员函数分配给变量吗?

是否应该关闭OutputStream对象(在方法调用中创建为表达式,但未分配给变量)?