By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

Deleting constant char* created using c_str()

P: 2
Hello, I wrote small function which translates text in my application:

map<string,string> localization;
1 string: universal identifier of localized text
2 string: text to localize
Expand|Select|Wrap|Line Numbers
  1. const char* App::localize(string local)
  2. {
  3.     if (!localization.count(local))
  4.     {
  5.         return "CANNOT FIND THIS TEXT";
  6.         }
  7.     else
  8.     {
  9.         return localization[string(local)].c_str();
  10.     }
  11. }
The problem is when I change language of my application at runtime -> It loads new values into "map<string,string> localization;" and erases the old ones so all constant char pointers become invalid.
May 27 '07 #1
Share this Question
Share on Google+
3 Replies


Savage
Expert 100+
P: 1,764
Hello, I wrote small function which translates text in my application:

map<string,string> localization;
1 string: universal identifier of localized text
2 string: text to localize
Expand|Select|Wrap|Line Numbers
  1. const char* App::localize(string local)
  2. {
  3.     if (!localization.count(local))
  4.     {
  5.         return "CANNOT FIND THIS TEXT";
  6.         }
  7.     else
  8.     {
  9.         return localization[string(local)].c_str();
  10.     }
  11. }
The problem is when I change language of my application at runtime -> It loads new values into "map<string,string> localization;" and erases the old ones so all constant char pointers become invalid.
Then don't use constant char pointers.

BTW,welcome to TSDN!

Savage
May 27 '07 #2

P: 2
Then don't use constant char pointers.
:-) thank you for this advice, but unfortunately I have to use them. I am using FLTK library for the gui and its widget labels must be constant char*s
May 27 '07 #3

Savage
Expert 100+
P: 1,764
:-) thank you for this advice, but unfortunately I have to use them. I am using FLTK library for the gui and its widget labels must be constant char*s
Is that function part of some class?

If it is,u can make a instance of the class when new language is selected,so that constant will remain valid.

Savage
May 27 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.