Search code examples
c++uwpc++-winrt

why outside the funtion the pointer data is not getting updated


I am new to c++ I am trying to update the similar variable using one function

I have a variable

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

then I have set function IComponent is and 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;
}

but my m_component1/2/3 is still Saying its null


Solution

  • It's because you're passing in oldComp as pointer by value. So setting this pointer to anything will only change the pointer locally in the function.

    Pass the pointer by reference if you want to affect the original pointer passed in.