【问题标题】:why outside the funtion the pointer data is not getting updated为什么在函数之外指针数据没有得到更新
【发布时间】:2021-09-17 00:22:24
【问题描述】:

我是 C++ 新手,我正在尝试使用一个函数更新类似的变量

我有一个变量

IComponent* m_component1;
IComponent* m_component2;
IComponent* m_component3;

然后我设置了函数IComponent is和interface

void SetComponent1(IComponent* comp) { SetComponent(comp, m_component1); }
void SetComponent2(IComponent* comp) { SetComponent(comp, m_component2); }
void SetComponent3(IComponent* comp) { SetComponent(comp, m_component3); }

void SetComponent(IComponent* newComp , IComponent* oldComp)
{
    if (oldComp)
    {
        oldComp->Clean();
    }
    newComp->Load();
    oldComp = newComp;
}

但我的 m_component1/2/3 仍然说它为空

【问题讨论】:

    标签: c++ uwp c++-winrt


    【解决方案1】:

    这是因为您将 oldComp 作为指针按值传递。因此,将此指针设置为任何内容只会在函数中本地更改指针。

    如果要影响传入的原始指针,则通过引用传递指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-17
      • 2021-09-29
      • 1970-01-01
      • 2021-02-18
      • 2022-07-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多