我正在阅读一些开源代码,并且对使用指针感到困惑。有人可以帮助我分析以下代码吗?

for (int i = 0; i < podRecords; i++)
{
    WaterRight *pRight = new WaterRight;
    pRight->m_reachComid = m_podDb.GetAsInt(m_colReachComid, i);
    int reachID = pRight->m_reachComid;


因此,我的理解是,通过创建new WaterRight,可以动态分配存储WaterRight成员地址的内存。然后,将m_colReachComid的值(或地址?)分配给m_reachComid,然后分配给reachID。我总是不确定哪个标识符是地址,哪个是值。例如,reachID是整数值,还是存储该值的地址?

最佳答案

reachIDint值。您将数字存储在那里。

pRight是指向WaterRight的指针。它存储一些WaterRight的地址。

pRight->m_reachComid也是int值。具体来说,它是由m_reachComid指向的WaterRight实例的pRight字段。

所以:

WaterRight *pRight = new WaterRight;
// pRight is a *pointer to a WaterRight instance*:
// you use pRight->m_reachComid to access the instance's m_reachComid field
pRight->m_reachComid = m_podDb.GetAsInt(m_colReachComid, i);

WaterRight right;
// right is a *WaterRight instance*
// you use pRight.m_reachComid to access the instance's m_reachComid field
pRight.m_reachComid = m_podDb.GetAsInt(m_colReachComid, i);

关于c++ - 成员和指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24896315/

10-09 20:10