0
void ClassA::Func()
{
    static map<int, string> mapIntStr;

    mapIntStr[0] = m_memberVariable0; <= just want to do once & m_memberVariable* are not static
    mapIntStr[1] = m_memberVariable1; <= just want to do once
    ...
}

I want to ONLY initialize the variable mapIntStr once. As we know, we can put the initialize code inside a static function and call that static function and store the return value into mapIntStr.

Here I would like to get a quick & dirty solution. As I remember, there is something called static scope. I would like to initialize mapIntStr ONCE without calling a static function. how can I do it?

Thank you

1
  • 2
    Is there any specific reason for this not to be a static member variable? Commented Mar 23, 2011 at 3:50

2 Answers 2

6
void ClassA::Func()
{
    static map<int, string> mapIntStr;

    if(mapIntStr.empty()){
      mapIntStr[0] = m_memberVariable0;
      mapIntStr[1] = m_memberVariable1;
      // ...
    }
}

How about that? :)
Edit
Well, the best solution would be to take the mapIntStr out of the function and into the class. And then you won't get around the static function call.

//in ClassA.h
class ClassA{
public:
  void Func();

  static map<int,string> InitStatic();
  static map<int,string> mapIntStr;
};

//in ClassA.cpp
#include "ClassA.h"
void ClassA::Func(){
  // use mapIntStr
}

map<int,string> ClassA::InitStatic(){
  map<int,string> ret;
  // init ret
  return ret;
}

map<int,string> ClassA::mapIntStr = ClassA::InitStatic();

That're about the only options that I know of. So you've got the choice. Initialize the map once in the func or even in a contructor the first time a ClassA object is created (better version of the two) and live with the overhead of a nearly noop call to empty(), which will most likely be a single instruction only after proper inlining and will impose no overhead whatsoever, or use a static initialization function.

Sign up to request clarification or add additional context in comments.

4 Comments

This is not the best solution b/c CPU will waste time to check the if condition -- thank you
Well of course.. the best solution would be something else, brb, editing it in.
Thank you very much for your proposal. As I mentioned in my question, I am looking for something different from calling a static function to initialize the mapIntStr. -- thank you
@q0987: you don't seem to realize how small a few CPU cycles are.
1

Create a inside struct, then initialize everything in the constructor of that struct, and then you declare a static variable of that struct.

void fun()
{
    struct setter
    {
         setter(){}
    };

    static setter setup;
}

For your case:

void ClassA::Func()
{
     struct Map : public std::map<int, string>
     {
          Map()
          {
              (*this)[0] = something;
              (*this)[1] = somethingElse;
          }
     }
     static Map map;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.