在c++中调用静态成员的方法进行初始化
Call a method of a static member for initialization in C++
我有一个具有静态成员对象的类。初始化静态对象意味着将它的一些参数设置为特定的值;但这是由那个物体的一个函数完成的。如果它是静态的,我不知道怎么做。任何帮助吗?
更具体地说,我每个类都有一个静态boost记录器对象。它有一个ClasName
属性,通过add_attribute("ClassName", boost::log::attributes::constant<std::string>("MyClass"))
函数将其设置为name_of_the_class
值。初始化静态日志记录器的最佳方法是什么?我已经做了:
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
我知道每次实例化类时都会这样做,所以:最好的方法是什么?
你可以初始化logger一次,例如在构造函数中使用静态变量:
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;
}
}
注意这不是线程安全的。但是,如果您不使用线程,它将工作,并且日志记录器将被初始化一次,但仅当您实例化MyClass
时。
如果BoostLogger
没有提供add_attribute
的构造函数,您可以创建自己的函数,如:
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");
首先看BOOST_LOG_GLOBAL_LOGGER
这是你的代码的更正版本。
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
不使用add_attribute
调用,而是使用它的构造函数完全初始化BoostLogger
。然后在定义它时简单地提供必要的参数。
相关文章:
- 初始化具有非默认构造函数的std::数组项的更好方法
- 在C++中初始化向量映射的最有效方法
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 初始化指向类实例的智能指针并访问其方法
- 不同的类或结构初始化方法之间的性能差异是什么?
- 初始化数组、"memset"或" {//value} "的最佳方法是什么?
- 在C++中全局初始化类的正确方法
- 在C++中使用默认构造函数初始化对象的不同方法
- 使用默认构造函数初始化对象的不同方法
- 初始化类的两种方法?
- C++初始化之前派生类调用基类的方法
- 初始化不是整数的巨大常量多维数组的最佳方法是什么?
- 仅通过C++中数组初始化的不同方法,即可在同一输入上获得两个不同的答案
- 使用静态方法初始化 const 类字段的做法是好是坏
- 是否可以使用自动类方法初始化自动变量?
- 指针类成员在通过类方法初始化后保持 NULL
- 从构造函数中本地声明的device_vector使用device_vector::data()方法初始化结构内部的指针是
- 使用什么方法初始化成员变量?
- 是否有更好的方法初始化引用成员以引用同一类中的另一个成员
- 混淆了定义自定义优先级队列和使用C++中的make_heap等方法初始化堆之间的区别