复习了一下c++ 的操作符重载以及 模板,随便写了一个demo 记录一下。
#include#define LOG(str) {printf("this is %s:%d t%sn",__FUNCTION__,__LINE__,str);} class MyOptr final { private: uint16_t *m_ptr; public: MyOptr(); void init(uint16_t *ptr); MyOptr(uint16_t *ptr); MyOptr(const MyOptr& val); operator uint16_t(); operator int16_t(); operator int32_t(); operator uint32_t(); operator float(); operator double(); operator uint64_t(); operator int64_t(); template operator T(); MyOptr& operator=(uint16_t val); MyOptr& operator=(int16_t val); MyOptr& operator=(int32_t val); MyOptr& operator=(uint32_t val); MyOptr& operator=(uint64_t val); MyOptr& operator=(int64_t val); MyOptr& operator=(double val); MyOptr& operator=(float val); template MyOptr& operator=(T val); MyOptr& operator<<(uint16_t val); MyOptr& operator<<(int16_t val); MyOptr& operator<<(int32_t val); MyOptr& operator<<(uint32_t val); MyOptr& operator<<(uint64_t val); MyOptr& operator<<(int64_t val); MyOptr& operator<<(double val); MyOptr& operator<<(float val); template MyOptr& operator<<(T val); MyOptr operator[](unsigned int index) const; }; typedef struct { int a; double b; }my_str; int main(void) { MyOptr temp; temp = (int)123; temp = (double)123; temp = (float)123; temp = (int64_t)123; temp = (uint64_t)123; temp = (int32_t)123; temp = (int16_t)123; temp = (uint16_t)123; printf("--------------------------1-------n"); temp << (int)123; temp << (double)123; temp << (float)123; temp << (int64_t)123; temp << (uint64_t)123; temp << (int32_t)123; temp << (int16_t)123; temp << (uint16_t)123; printf("--------------------------2-------n"); {int val = temp;} {double val = temp;} {float val = temp;} {int64_t val = temp;} {uint64_t val = temp;} {int32_t val = temp;} {int16_t val = temp;} {uint16_t val = temp;} printf("--------------------------3-------n"); temp[123] = (uint16_t)123; printf("--------------------------4-------n"); my_str m; temp << m; temp = m; } template MyOptr::operator T() { LOG("template MyOptr::operator T"); return T{0}; } template MyOptr& MyOptr::operator=(T val) { LOG("template MyOptr::operator="); } template MyOptr& MyOptr::operator<<(T val) { LOG("template MyOptr::operator<<"); } MyOptr::MyOptr() { LOG("MyOptr()"); } void MyOptr::init(uint16_t *ptr) { LOG("MyOptr::init(uint16_t *ptr)"); } MyOptr::MyOptr(uint16_t *ptr) { LOG("MyOptr(uint16_t *ptr)"); } MyOptr::MyOptr(const MyOptr& val) { LOG("MyOptr(const MyOptr& val)"); } MyOptr::operator uint16_t() { LOG("MyOptr::operator uint16_t()"); return 0; } MyOptr::operator int16_t() { LOG("MyOptr::operator int16_t()"); return 0; } MyOptr::operator int32_t() { LOG("MyOptr::operator int32_t()"); return 0; } MyOptr::operator uint32_t() { LOG("MyOptr::operator uint32_t()"); return 0; } MyOptr::operator float() { LOG("MyOptr::operator float()"); return 0; } MyOptr::operator double() { LOG("MyOptr::operator double()"); return 0; } MyOptr::operator uint64_t() { LOG("MyOptr::operator uint64_t()"); return 0; } MyOptr::operator int64_t() { LOG("MyOptr::operator int64_t()"); return 0; } MyOptr& MyOptr::operator=(uint16_t val) { LOG("MyOptr& MyOptr::operator=(uint16_t val)"); } MyOptr& MyOptr::operator=(int16_t val) { LOG("MyOptr& MyOptr::operator=(int16_t val)"); } MyOptr& MyOptr::operator=(int32_t val) { LOG("MyOptr& MyOptr::operator=(int32_t val)"); } MyOptr& MyOptr::operator=(uint32_t val) { LOG("MyOptr& MyOptr::operator=(uint32_t val)"); } MyOptr& MyOptr::operator=(uint64_t val) { LOG("MyOptr::operator=(uint64_t val)"); } MyOptr& MyOptr::operator=(int64_t val) { LOG("MyOptr::operator=(int64_t val)"); } MyOptr& MyOptr::operator=(double val) { LOG("MyOptr::operator=(double val)"); } MyOptr& MyOptr::operator=(float val) { LOG("MyOptr::operator=(float val)"); } MyOptr MyOptr::operator[](unsigned int index) const { LOG("MyOptr::operator[](unsigned int index) const"); return *this; } MyOptr& MyOptr::operator<<(uint16_t val) { LOG(" operator<<(uint16_t val);"); } MyOptr& MyOptr::operator<<(int16_t val) { LOG(" operator<<(int16_t val);"); } MyOptr& MyOptr::operator<<(int32_t val) { LOG(" operator<<(int32_t val);"); } MyOptr& MyOptr::operator<<(uint32_t val) { LOG(" operator<<(uint32_t val);"); } MyOptr& MyOptr::operator<<(uint64_t val) { LOG(" operator<<(uint64_t val);"); } MyOptr& MyOptr::operator<<(int64_t val) { LOG(" operator<<(int64_t val);"); } MyOptr& MyOptr::operator<<(double val) { LOG(" operator<<(double val);"); } MyOptr& MyOptr::operator<<(float val) { LOG(" operator<<(float val);"); }
root@jeason:~/cpp# g++ 1.cpp root@jeason:~/cpp# ./a.out this is MyOptr:118 MyOptr() this is operator=:182 MyOptr& MyOptr::operator=(int32_t val) this is operator=:198 MyOptr::operator=(double val) this is operator=:202 MyOptr::operator=(float val) this is operator=:194 MyOptr::operator=(int64_t val) this is operator=:190 MyOptr::operator=(uint64_t val) this is operator=:182 MyOptr& MyOptr::operator=(int32_t val) this is operator=:178 MyOptr& MyOptr::operator=(int16_t val) this is operator=:174 MyOptr& MyOptr::operator=(uint16_t val) --------------------------1------- this is operator<<:220 operator<<(int32_t val); this is operator<<:236 operator<<(double val); this is operator<<:240 operator<<(float val); this is operator<<:232 operator<<(int64_t val); this is operator<<:228 operator<<(uint64_t val); this is operator<<:220 operator<<(int32_t val); this is operator<<:216 operator<<(int16_t val); this is operator<<:212 operator<<(uint16_t val); --------------------------2------- this is operator int32_t:144 MyOptr::operator int32_t() this is operator double:159 MyOptr::operator double() this is operator float:154 MyOptr::operator float() this is operator int64_t:169 MyOptr::operator int64_t() this is operator uint64_t:164 MyOptr::operator uint64_t() this is operator int32_t:144 MyOptr::operator int32_t() this is operator int16_t:139 MyOptr::operator int16_t() this is operator uint16_t:134 MyOptr::operator uint16_t() --------------------------3------- this is operator[]:206 MyOptr::operator[](unsigned int index) const this is MyOptr:130 MyOptr(const MyOptr& val) this is operator=:174 MyOptr& MyOptr::operator=(uint16_t val) --------------------------4------- this is operator<<:113 templateMyOptr::operator<< this is operator=:107 template MyOptr::operator=



