C++指针问题

发布于 2024-09-24 04:33:55 字数 1639 浏览 5 评论 0原文

我遇到了一个我似乎无法弄清楚的指针问题。看起来我已经以这种方式使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

維他命╮ 2024-10-01 04:33:55

如果您想修改原始指针,您的函数应该接收指向指针的指针。

否则,您只获得指针值的副本并修改该副本。

// will modify the copy
void ptr( MyClass * ptr_copy )
{
    ptr_copy = new MyClass();
}

// will modify the original ptr
void ptr2ptr( MyClass ** ptr_2_ptr )
{
    *ptr_2_ptr = new MyClass();
}

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.

// will modify the copy
void ptr( MyClass * ptr_copy )
{
    ptr_copy = new MyClass();
}

// will modify the original ptr
void ptr2ptr( MyClass ** ptr_2_ptr )
{
    *ptr_2_ptr = new MyClass();
}
偏爱自由 2024-10-01 04:33:55

如果您希望对指针的更改反映在调用者中,请通过引用传递指针。

#include <iostream>
using namespace std;

void f(int *pnochange, int *&pchange){
   pnochange++;
   pchange++;
}

int main(){
   int buf[] = {1, 2};

   int *p1, *p2;
   p1 = p2 = buf;

   f(p1, p2);

   cout << *p1 << *p2;    // prints 12
}

Pass the pointers by reference if you wish the changes to the pointers to be reflected in the caller.

#include <iostream>
using namespace std;

void f(int *pnochange, int *&pchange){
   pnochange++;
   pchange++;
}

int main(){
   int buf[] = {1, 2};

   int *p1, *p2;
   p1 = p2 = buf;

   f(p1, p2);

   cout << *p1 << *p2;    // prints 12
}
若水般的淡然安静女子 2024-10-01 04:33:55

通过引用传递

Pass by Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文