简体   繁体   English

使用结构的字段,同时使用其地址

[英]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'.我需要访问他们的 matrixPos 字段以获取 if 语句中所需的值,同时我还需要他们的地址以获取字段“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.如果您要做的只是从expandNode function 中获取指向nodToExpand的指针,则可以通过获取其地址来实现。 References can be used as if they where the referred object itself, that's their whole deal.引用可以像引用 object 本身一样使用,这就是他们的全部交易。

All you need is a good old &nodToExpand .您所需要的只是一个好的旧&nodToExpand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM