c++奇怪的搞法汇总

来源:互联网 发布:python 程序 编辑:程序博客网 时间:2024/05/01 14:58

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

down votefavorite

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


原创粉丝点击