简体   繁体   中英

Call a method of a static member for initialization in C++

I have a class that has a static member object. Initializing that static object means setting some of its parameter to a specific value; but this is done by a function from that object. i do not know how to do it if it is static. Any help?


To be more specific I have a static boost logger object per class. It has a ClasName attribute and setting this to the name_of_the_class value is done by add_attribute("ClassName", boost::log::attributes::constant<std::string>("MyClass")) function. What is the best way to initialize the static logger? I have done:

typedef boost::log::sources::severity_logger< severity_levels > BoostLogger;

class MyClass
{
private:
  static BoostLogger m_logger;

public:
  MyClass()
  {
    MyClass::m_logger.add_attribute("ClassName", boost::log::attributes::constant<std::string>("MyClass"));
  }
}

BoostLogger MyClass::m_logger; // And here I cannot call the add_attribute() function

I know that this is done every time I instantiate the class, so: What is the best way to do it?

You can initialise the logger once eg with a static variable in your constructor:

MyClass()
{
  static bool logger_initialised = false;
  if (!logger_initialised)
  {
    MyClass::m_logger.add_attribute("ClassName", boost::log::attributes::constant<std::string>("MyClass"));
    logger_initialised = true;
  }
}

Note that this is not thread-safe. But if you do not use threads, it will work and the logger will be initialised one time but only if you instantiate MyClass .

if BoostLogger doesn't provide the constructor to add_attribute you may create your own function for that, something like:

class MyClass
{
private:
    static BoostLogger m_logger;
};

BoostLogger CreateBoostLoggerWithClassName(const std::string& className)
{
    BoostLogger logger;
    logger.add_attribute(
        "ClassName",
        boost::log::attributes::constant<std::string>(className));
    return logger;
}

BoostLogger MyClass::m_logger = CreateBoostLoggerWithClassName("MyClass");

First of all look at the BOOST_LOG_GLOBAL_LOGGER

Here is corrected version of your code.

MyClass.h:

class MyClass
{
private:
  static BoostLogger m_logger;  /*  This is declaration, not definition.
                                    You need also define the member somewhere */

public:
  MyClass() /*  So far as this is constructor, it may be called more than once,
                I think adding new static function will be better */
  {
    // MyClass::m_logger.add_attribute("ClassName", boost::log::attributes::constant<std::string>("MyClass"));
    // AddAttribute("ClassName"); Uncomment this line if you're really need to add attribute in constructor
  }

  static void AddAttribute(std::string name) /* this function has been added as advice,
                                                if you're going add attributes as you
                                                did in question,
                                                remove function, and uncomment first              line in the constructor */
  {
    MyClass::m_logger.add_attribute(name, boost::log::attributes::constant<std::string>("MyClass"));
  }
}

MyClass.cpp:

BoostLogger MyClass::m_logger = BoostLogger(); // This is definition of static member item

Instead of an add_attribute call, make it so that you can fully initialise BoostLogger with its constructor. Then simply provide the necessary argument when you define it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM