0

I am trying to create a map of static objects map<string, base*>, where base is derived by classes class1 and class2. My map will store pointers to objects of type class1 and class2.

After reading a few posts I decided to do this by creating a singleton with a static class function to initialize the map:

class A {  // singleton class
public:

static map<string, base*> create_map() {
map<string,base*> m;
m["1"]=new class1();
m["2"]=new class2();
};
static const map<string, base*> myMap;
};

int main() {    
const map<string,base*> A::myMap = A::create_map();
myMap["1"]->func();    
}

However this gives an error: myMap is not declared in this scope. Can somebody explain the singleton method and what I am doing wrong. How would this change if I have a separate header file ?
TIA.

EDIT: Changed the code to:

class A {  // singleton class
public:

static map<string, base*> create_map() {
map<string,base*> m;
m["1"]=new class1();
m["2"]=new class2();
return m;
};
static const map<string, base*> myMap;
};

const map<string,base*> A::myMap = A::create_map();
int main() {    

A::myMap["1"]->func();    
}

This still gives an error: passing const std::map<std::basic_string<char>, base*> as 'this' discards qualifiers.

Jagadeesh
  • 263
  • 2
  • 13
  • Regarding singletons: why do you need one? What about simply calling your function only once (and passing it a reference to the map)? – AVH Oct 23 '17 at 08:30
  • create_map() returns map you dont return anything... am I missing something? where `m` is declared on the stack and gets destroyed at the end of your `create_maps` scope – Samer Tufail Oct 23 '17 at 08:33
  • Also note that 1. the `create_map` function doesn't return the created map. 2. You are leaking memory because of using raw pointers. Using `std::unique_ptr` will solve that. Arguably it's not too bad here, because the leak happens when your program exits, so the OS will clean it up, but still. – AVH Oct 23 '17 at 08:33
  • `const map A::myMap = A::create_map();` should be outside of `main` – M.M Oct 23 '17 at 08:34
  • @Darhuuk : can you please give an example of what you mean when you say 'simply calling your function' ? – Jagadeesh Oct 23 '17 at 08:39
  • If your `map` is `const`, use `.at("1")` instead of `["1"]`. – Jarod42 Oct 23 '17 at 09:41
  • @Jagadeesh Well, instead of trying to enforce that you have a singleton, how about just creating the object once? Does that make your code easier? See also: https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – AVH Oct 23 '17 at 12:07

2 Answers2

0

myMap is a member of A, you need to address it accordingly.

A::myMap["1"]->func();    
Eran
  • 349
  • 2
  • 10
0

You should add the line map<string, base*> A::myMap; after your class. You should also avoid the const declaration of your map object, and create_map() method was lacking of a return statement

class A {  // singleton class
public:

    static map<string, base*> create_map() {
        map<string,base*> m;
        m["1"] = new class1();
        m["2"] = new class2();

        return m;
    }

    static map<string, base*> myMap;
};

map<string, base*> A::myMap;

int main() {
    A::myMap = A::create_map();
    A::myMap["1"]->func();

    return 0;
}