Is it safe to use const char * literal as a std::map key?

Viewed 658

I have classes, each of them return its name

struct IFoo {
    virtual const char * GetName() const = 0;
}

struct Foo : IFoo {
   const char * GetName() const { return "Foo"; }
}

struct Bar: IFoo {
   const char * GetName() const { return "Bar"; }
}

And somewhere else:

Foo* a = new Foo();
Foo* b = new Foo();

std::map<const char *, int> data;
data[a->GetName()] = 0;
printf("%i", data[b->GetName()]);

string literals should be stored in one place at memory, but is it 100%? This code works in gcc, but I am not sure of its multi-platformness.

1 Answers
Related