十六进制转十进制(进制转化)
#include <sstream>
#include <string>
using namespace std;
int main()
{
int nOut = 0;
string name = "d0"; // 十六进制
stringstream ss;
ss << hex << name ;
ss >> nOut; // 转化后的十进制为208
return 1;
}
计算结构体首地址
#incldue <iostream>
using namespace std;
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER) //获取MEMBER的偏移量
#define container_of(ptr, type, member) {\
(type*)( (char*)__mptr - offsetof(type, member) )};
struct ST_TEST{
int a;
float b;
};
int main()
{
ST_TEST stTest;
ST_TEST *stTest1 = container_of(&stTest.b, ST_TEST, b);
cout << "using define:" << stTest1 << endl
<< "using & :" << &stTest.a << endl;
return 0;
}
在构造函数中为什么更倾向于使用member initial list?
使用member initial list如下的第一种情况,因为这种方式优先于对象创建,更快。基于这个原则,const变量必须在成员初始化列表里进行,否则,到了构造函数内部,进行赋值会失败。——初始化和赋值的不同之处。
class Date{
int year;
int month;
int day;
const int nTestAssign;
public:
Date(int x, int y, int z)
: year(x) // 1
, month(y)
, day(z)
, nTestAssign(0) // OK
{
year = x; // 2
month = y;
day = z;
nTestAssign = 0; // error!!
}
};