#include <stdlib.h>
#include <iostream>
// #include <stddef.h>
#define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(PTR,TYPE,MEMBER) ({ \
const typeof(((TYPE *)0)->MEMBER) *__mptr=(PTR); \
(TYPE *) ((char *)__mptr - offsetof(TYPE,MEMBER)); })
using namespace std;
struct sNode{
int a;
int b;
char c;
sNode():a(1),b(2){}
sNode(int a,int b):a(3),b(4){}
};
class cNode{
public:
int a;
int b;
int d;
cNode(int a,int b):a(a),b(b),c(11){}
private:
int c;
int f;
public:
int p(){return 1;}
int e;
};
int main()
{
sNode s;
sNode s1(1,2);
cNode c(1,2);
cout << s.a << c.b << s1.a << endl;
cout << offsetof(sNode, b) << endl;
cout << offsetof(cNode, d) << endl;
// cout << offsetof(cNode, c) << endl;//无法访问
cout << offsetof(cNode, e) << endl;
int *p = (int*)&c.a;
cNode *ptt=container_of(p,cNode,a);//通过成员的地址来得到类或结构体的首地址
cout << ptt->a << endl;
char* pc =(char*) ptt;
cout << (*((int*)(pc+12))) << endl;//输出了c的内容
//cout << c.c << endl;//自然是错误的
return 0;
}
//参考:https://www.cnblogs.com/litifeng/p/7690585.html
c++的类这么久自然说明上面的代码没啥意义,不过熟悉下整个流程还是好的