Use the fields of a struct while also using its address

struct nod {
    std::pair<int, int> matrixPos;
    nod *anteriorNode;

    nod(std::pair<int, int> matrixPos)
        matrixPos = matrixPos;
        anteriorNode = nullptr;
    } //this is for the first node

    nod(std::pair<int, int> matrixPos, nod* anteriorNode)
        matrixPos = matrixPos;
        anteriorNode = anteriorNode;

void expandNode(std::queue<nod> &coada, int **matrice, nod& nodToExpand)
    if (matrice[nodToExpand.matrixPos.first-1][nodToExpand.matrixPos.second] == 0)
        nod newNode = nod(std::pair<int, int>(nodToExpand.matrixPos.first - 1, nodToExpand.matrixPos.second), nodToExpand);

I have some nodes that I need to link together (so I know which is the parent of each node) while also accessing their values.

I need to acess their matrixPos fields for the values needed in the if statement, while I also need their address for the field ‘anteriorNode’.

How can I do this and what is the most efficient way?


If all you are trying to do is obtain a pointer to nodToExpand from within the expandNode function, you can do so by getting its address. References can be used as if they where the referred object itself, that’s their whole deal.

All you need is a good old &nodToExpand.

Leave a Reply

Your email address will not be published. Required fields are marked *