A General copy constructor question

Started by
4 comments, last by Alberth 8 years, 7 months ago

When creating a QuadNode by giving it an id and t (time variable),

I want to use back the original TimeAStarNode and QuadNode properties.

Do I have to call back TimeAStarNode constructor like this and all other QuadNode properties will retain?

Thanks

Jack


class QuadNode : public TimeAStarNode
{
public:

    QuadNode(const quad::vec2* bb, QuadTree* tree, QuadNode* parent, const Point<int>& top_right, const Point<int>& bottom_left, float fHeightThresHold, bool bRegularGridMode = false, int currentDepth = 0, int maxDepth = 0, bool init = false);

    QuadNode(const QuadNode& node)
        // ancestor is used to build quadtree, which is the parent of current node
        : TimeAStarNode(node.ancestor, node.m_quadNode, node.path_cost, node.estimated_cost, node.t, node.depth)
    {
        // overwrites current id
        this->id = node.id;

        // used to build paths
        this->parent = node.parent; 
        this->m_pivot.x = node.m_pivot.x;
        this->m_pivot.y = node.m_pivot.y;
        this->m_boundingBox[0] = node.m_boundingBox[0];
        this->m_boundingBox[1] = node.m_boundingBox[1];

        this->m_vSize = node.m_vSize;
        this->m_vPos = node.m_vPos;

        this->m_tree = node.m_tree;
        this->m_bitRegions = node.m_bitRegions;
        this->m_bRegularGridMode = node.m_bRegularGridMode;

        this->m_fCurrentSize = node.m_fCurrentSize;

        ///////    
        this->northeast = node.northeast;
        this->northwest = node.northwest;
        this->southeast = node.southeast;
        this->southwest = node.southwest;
        this->status = node.status;
        this->m_OverHangTris = node.m_OverHangTris;
 
        this->transitions = node.transitions;

    }

    
public:
    QuadNode(int id, long t)
        : TimeAStarNode(this->ancestor, this->m_quadNode, this->path_cost, this->estimated_cost, this->t, this->depth)
    {
        // overwrites current id
        this->id = id;
        this->parent = NULL; ;
        this->transition = 0;
    }
Advertisement

QuadNode(int id, long t)
: TimeAStarNode(this->ancestor, this->m_quadNode, this->path_cost, this->estimated_cost, this->t, this->depth)
{
// overwrites current id
this->id = id;
this->parent = NULL; ;
this->transition = 0;
}

No, this will pass the un-initialized/unknown attributes to the parent class. This will not really work.

Something like this would do


    QuadNode(const QuadNode& node,int id, long t)
        : TimeAStarNode(node.ancestor, node.m_quadNode, node.path_cost, node.estimated_cost, node.t, node.depth)
    {
// optional standard copy constructor code from above...


        // overwrites current id
        this->id = id;
        this->parent = NULL; ;
        this->transition = 0;
    }

Or, if you just want to reassign a new id and t, then add an simple reassign method:


void reassign(int id, long t)
    {
        // overwrites current id
        this->id = id;
        this->parent = NULL; ;
        this->transition = 0;
    }

Thanks, it really looks neater..

Jack

Hey there.

Are you using the this pointer in a constructor.

Ready here.

While the OP is using 'this' far too often even in places where he absolutely does not need to, his uses are not dangerous in the sense of the article you linked. He's not doing anything the compiler does not do itself automatically.
'this' is used as "I am accessing a member" annotation, I do that as well.

This topic is closed to new replies.

Advertisement