C++ - map<int, object> Dictionary object with reference problem

Markus Freitag 3,791 Reputation points
2022-03-16T12:09:01.043+00:00

Hello,

I have two maps. Please look there ///******** change is not inside the first map.

map<int, CInfos> DicIndexInfos;
map<int, CInfos> DicIndexInfosCurrentPanel;



infos.ProductID = infos.ProductID.Trim();
infos.MaterialSN = infos.MaterialSN.Trim();
infos.AssemblySN = infos.AssemblySN.Trim();
infos.State = infos.State.Trim();

DicIndexInfos[index] = infos;

if (infos.State.MakeLower() != "used" && isCurrentPanelFilled == false)
{
    // *** Fill current panel
    counterIndexCurrentPanel++;
    infos.State = "used";    ///******** change is not inside the first map.

    DicIndexInfosCurrentPanel[counterIndexCurrentPanel] = infos;

How I can solve this?

When I change something, the first map does not have the changed state. How do I do this as a reference?
In C# I know this differently.

Thanks.

Developer technologies C++
Developer technologies .NET .NET CLI
0 comments No comments
{count} votes

Accepted answer
  1. Guido Franzke 2,191 Reputation points
    2022-03-16T12:43:48.763+00:00

    Hello,
    you could use infos as a reference variable in your first map, e.g.:

    CInfos& infos = DicIndexInfos[index];
    
     infos.ProductID = infos.ProductID.Trim();
     infos.MaterialSN = infos.MaterialSN.Trim();
     infos.AssemblySN = infos.AssemblySN.Trim();
     infos.State = infos.State.Trim();
    
     if (infos.State.MakeLower() != "used" && isCurrentPanelFilled == false)
     {
         // *** Fill current panel
         counterIndexCurrentPanel++;
         infos.State = "used";  
    

    Regards, Guido

    Edit: have a look at this post: is-it-safe-to-get-an-object-in-stdmap-by-reference

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.