我正在阅读一些开源代码,并且对使用指针感到困惑。有人可以帮助我分析以下代码吗?
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
是整数值,还是存储该值的地址? 最佳答案
reachID
是int
值。您将数字存储在那里。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/