Sign in to follow this  

A General copy constructor question

This topic is 816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
    }
Edited by lucky6969b

Share this post


Link to post
Share on other sites

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;
    }
Edited by Ashaman73

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this