C++指针问题
我遇到了一个我似乎无法弄清楚的指针问题。看起来我已经以这种方式使用了 1000 次指针,所以我不太确定这里发生了什么。我有以下代码:
int iRetVal;
CycleCountOrder* cycleOrder = NULL;
CycleCountLineItem* cycleLine = NULL;
iRetVal = m_CycleCount.GetCCOrderLine(pOneLocation.szOrderNum[c], cycleOrder, cycleLine);
每当我调用 GetCCOrderLine 时,我都会进入该函数,并将有效值分配给指针 CycleOrder 和 CycleLine。当我跳出 GetCCOrderLine 函数时,引用再次变为 NULL。下面的代码是 GetCCOrderLine 的定义方式:
头文件
int GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine);
cpp 文件
int CCycleCount::GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine)
{
CString szCurrOrderLnitem;
for(int c = 0; c < m_Orders.GetCount(); c++)
{
CycleCountOrder* currentOrder = m_Orders[c];
for(int d = 0; d < currentOrder->m_LineItems.GetCount(); d++)
{
CycleCountLineItem* currentLine = currentOrder->m_LineItems[d];
szCurrOrderLnitem.Format("%s-%d-%d", currentOrder->szOrderNum, currentLine->nLnitemNum, currentLine->nSubitemNum);
if(szCurrOrderLnitem == szOrderLnitem)
{
cycleOrder = currentOrder;
cycleCountLine = currentLine;
return FUNC_OK;
}
}
}
return FUNC_ERROR;
}
另外,在上述代码中访问的两个数组以及用于为传入的指针赋值的两个数组声明如下。这些数组还填充了使用 new 运算符创建的对象:
CArray<CycleCountOrder*, CycleCountOrder*> m_Orders;
CArray<CycleCountLineItem*, CycleCountLineItem*> m_LineItems;
I'm having a pointer problem that I can't seem to figure out. It seems like I've used pointers in this way a 1000 times so I'm not quite sure what is going on here. I have the following code:
int iRetVal;
CycleCountOrder* cycleOrder = NULL;
CycleCountLineItem* cycleLine = NULL;
iRetVal = m_CycleCount.GetCCOrderLine(pOneLocation.szOrderNum[c], cycleOrder, cycleLine);
Whenever I call GetCCOrderLine I step inside the function and it assigns valid values to the pointers cycleOrder and cycleLine. When I step outside of the function GetCCOrderLine the references are NULL again. The code below is how the GetCCOrderLine is defined:
header file
int GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine);
cpp file
int CCycleCount::GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine)
{
CString szCurrOrderLnitem;
for(int c = 0; c < m_Orders.GetCount(); c++)
{
CycleCountOrder* currentOrder = m_Orders[c];
for(int d = 0; d < currentOrder->m_LineItems.GetCount(); d++)
{
CycleCountLineItem* currentLine = currentOrder->m_LineItems[d];
szCurrOrderLnitem.Format("%s-%d-%d", currentOrder->szOrderNum, currentLine->nLnitemNum, currentLine->nSubitemNum);
if(szCurrOrderLnitem == szOrderLnitem)
{
cycleOrder = currentOrder;
cycleCountLine = currentLine;
return FUNC_OK;
}
}
}
return FUNC_ERROR;
}
Also the two arrays that are being accessed in the above code and that are being used to assign values to the pointers passed in are declared as follows. Also these arrays are filled with objects created with the new operator:
CArray<CycleCountOrder*, CycleCountOrder*> m_Orders;
CArray<CycleCountLineItem*, CycleCountLineItem*> m_LineItems;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想修改原始指针,您的函数应该接收指向指针的指针。
否则,您只获得指针值的副本并修改该副本。
Your function should receive pointer to pointer if you want to modify the original pointer.
Otherwise you just get the copy of the pointer values and you modify this copy.
如果您希望对指针的更改反映在调用者中,请通过引用传递指针。
Pass the pointers by reference if you wish the changes to the pointers to be reflected in the caller.
通过引用传递
Pass by Reference