0

I am working on 2 files, lets take file1.cpp and file2.cpp. file1.cpp contains:

//File 1
#include <iostream>
#include <map>

struct category {
    int id;
};

void fun();

std::map<char, category> mymap1;
static std::map<char, category> mymap;
std::map<char, category>::iterator map_iter;
std::map<char, category>::iterator map_iter1;

int main ()
{

    mymap1['a'] = {20};
    mymap1['b'] = {30};
    mymap1['c'] = {40};

    for(int i = 0;i < 4; i++)
        fun();

    return 0;
}

//File 2
#include<file2.h>
void fun()
{
     mymap = mymap1;
     map_iter = mymap.begin();
     for (map_iter1 = mymap1.begin(); map_iter1 != mymap1.end();++map_iter1)
     {
        map_iter->second.id = map_iter1->second.id - map_iter->second.id;
        std::cout<<map_iter1->second.id<<"  " <<map_iter->second.id;
        map_iter->second.id=map_iter1->second.id;
        ++map_iter;
     }

}

I am trying to initialize mymap once when for loop run for the first time after that initialization should not happen as it happen in static variable.

But this behaviour is not shown and each time mymap is getting initialize.

I don't want the "id" getting initialize every time.

How to work?

halfer
  • 19,824
  • 17
  • 99
  • 186
vishal kumar
  • 43
  • 1
  • 9

3 Answers3

0

If I understand your question correctly, you should be checking before assigning anything to mymap in your fun() function if you only want to initialize it once.

For example, you could pass a variable by reference in your function by adding a parameter, let's call it foo. You initialize it to 0 before your for loop, add a check in your function to make sure that foo == 0 and increment it at the end.

Olivier Poulin
  • 1,778
  • 8
  • 15
0

Create a function that returns a static map

static std::map<char, category> maps() {
    static std::map<char, category> m;
    //...
    return m;
}
andre
  • 7,018
  • 4
  • 43
  • 75
  • Caution on the `static` in front: http://stackoverflow.com/questions/6953248/static-nonmember-or-static-nonmember-function – davidhigh Jun 10 '15 at 14:52
  • Keep in mind that depending on your compiler this may result in a function that is not thread safe. The version of Visual Studio I used, for example, would suffer an access violation here if two threads entered the fucntion for the first time very close together. Perhaps that has been remedied in C++1x since it now deals with concurrency explicitly. – BeeOnRope Sep 17 '15 at 20:13
  • 1
    According to §6.7.4, initialization of static variables is thread-safe, implementation quality vary though. – sp2danny Sep 17 '15 at 20:24
0

in general 'initialize once' is done like:

void foo()
{
    static bool first = true;

    if( first )
    {
        first = false;
        // first time init here
    }

    // rest of func here
}

Also a note on what static means:

static on a global variable:
* only accessible in local translation unit

static on a function:
* only accessible in local translation unit

static on a local variable:
* persists through calls, only one instance even on multiple/recursive call

static on a class/struct member:
* only one instance, regardless of number of parent objects

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • This approach is not thread safe, and so is appropriate only in a process you know to be single-threaded. – BeeOnRope Sep 17 '15 at 23:23