1 静态局部变量
int WorkEvent::GenerateId()
{
static int uiEventId = 0;
return uiEventId++;
}
2 define and enum 预编译是顺序的,不能随便调整
#define MAKE_UNION(VAR) VAR #define MAKE_STRINGS(VAR) #VAR
#define APP_EVENT_TYPE(DO) DO(DIGIT), DO(DIGIT2) typedef enum { APP_EVENT_TYPE(MAKE_UNION) }APP_EVENT_NAME;
const string g_strAryAppEventName[EVENT_NAME_END+1] = { APP_EVENT_TYPE(MAKE_STRINGS) };
3 delete
#define SAFE_DELETE(p) {if(NULL != p) {delete p; p = NULL;}} #define SAFE_DELETE_ARRAY(p) {if(NULL != p) {delete [] p; p = NULL;}}
4
Before C++11, we could only perform in-class initialization on static const members of integral or enumeration type.Stroustrup discusses this in his C++ FAQ , giving the following example:
class Y {
const int c3 = 7 ; // error: not static
static int c4 = 7 ; // error: not const
static const float c5 = 7 ; // error: not integral
};
5 类型转化
#include <string> #include <iostream> #include <sstream>
int main(void) { std::string str; std::stringstream stream; stream<<"1"<<2; stream>>str;
std::cout<<str<<std::endl;
return 0; }
6 string *ptString1 = false;
//string *ptString1=true; compile error string *ptString1 = false; if (!ptString1) { cout << "true1" << endl; } string *ptString2 = NULL; if (!ptString2) { cout << "true2" << endl; } string *ptString3 = new string; if (ptString3) { cout << "true3" << endl; } int x = 0; if (!x) { cout << "true4" << endl; } int y = 1; if (y) { cout << "true5" << endl; }
java all compile error