C++???????????????????
???????????? ???????[ 2015/3/10 11:20:51 ] ????????C++ ??????? ????
????Nifty counter.
???????????????????? Nifty counter?? ???? GCC ??????????????[3]????????????????????????????? x?? Nifty counter ???????????????????????????????? x ????????????? static ?????????????? static ????????????????????????????????????? x?????????????????????? x????????£?
// global.h
#ifndef _global_h_
#define _global_h_
extern X x;
class initializer
{
public:
initializer()
{
if (s_counter_++ == 0) init();
}
~initializer()
{
if (--s_counter_ == 0) clean();
}
private:
void init();
void clean();
static int s_counter_;
};
static initializer s_init_val;
#endif
????? cpp ?????
// global.cpp
#include "global.h"
static X x;
int initializer::s_counter_ = 0;
void initializer::init()
{
new(&x) X;
}
void initializer::clean()
{
(&x)->~X();
}
????????????????????????? x ????????????? global.h ???????????????????????????????????? initializer ??????????????? s_init_val?? ????????????????????????????????????????????? s_init_val?????????????? s_init_val ????????????? x ????????????????????? s_init_val ????????????? x ?????????????? initializer ?????????????????? s_counter_ ????????????????????????????????????
????????? x ????? placement new ???????????????????????????????????????????????δ??????????????Щ???????????????????????????Щ??????????
???????????? x ???????????????????????????????init() ????????? x ??????????? x ????????????x ??????????????????Σ???? x ??????????????????Σ??????????????????????????????? x ????????
// global.cpp
#include "global.h"
static char g_dummy[sizeof(X)];
static X& x = reinterpret_cast<X&>(g_dummy);
int initializer::s_counter_ = 0;
void initializer::init()
{
new(&x) X;
}
void initializer::clean()
{
(&x)->~X();
}
???????? static X& x = reinterpret_cast<X&>(g_dummy); ??????????????????? g_dummy ??????????????? x ???????????????????????????????????????? x ????????????????????????????????????????????????????????????????????? ?????????????????????????????????????? x ???????? include ???? global.h????????????????? y ?????????????????????? x?? ???????????????????? foo???? foo ???????? x?????????????????? y ????????????????????? x???????п?????? include ???? global.h????? y ?????????п????? x ????????
????????????? gcc c++ ???????????е????????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??C++????????????C++ lvalue??rvalueC++11????????C++???????????????C++?е?????????????????C++?????????C++???Windows????λ??C/C++???????????????????JAVA??C??C++??????????c++??python???????????????????????????????C++???????C++?е????????C++????????????????C++ ???????????????C++?????????????????????C++????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????