鱼C论坛

 找回密码
 立即注册
查看: 1499|回复: 3

[技术交流] C++无聊瞎写的

[复制链接]
发表于 2019-9-12 02:01:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 bin554385863 于 2019-9-12 02:03 编辑
  1. #include <iostream>
  2. template <typename t>
  3. class point
  4. {
  5. private:
  6.     t val;

  7. public:
  8.     point():val(0){}
  9.     point(t var):val(var){}
  10.     point(t* ptr):val(*ptr){}
  11.     point(const point& p):val(p.val){}

  12.     t *getAddr()
  13.     {
  14.         return &val;
  15.     }
  16.     t getValue()
  17.     {
  18.         return val;
  19.     }
  20.     void setValue(t var)
  21.     {
  22.         *(&val) = var;
  23.     }
  24.     ~point(){}
  25. };

  26. int main(int argc, char const *argv[])
  27. {
  28.     using namespace std;
  29.     int a = 100, b = 54;
  30.     int *p;
  31.     p = &b;
  32.     cout<<"a = "<<&a<<"---"<<a<<'\n'<<endl;
  33.     point<int> ptr;
  34.     cout<<"ptr默认数据"<<endl;
  35.     cout<<"ptr = "<<ptr.getAddr()<<"---"<<ptr.getValue()<<'\n'<<endl;
  36.     ptr = a;
  37.     cout<<"ptr = a"<<endl;
  38.     cout<<"ptr = "<<ptr.getAddr()<<"---"<<ptr.getValue()<<'\n'<<endl;
  39.     ptr.setValue(52.0);
  40.     cout<<"ptr.setValue(52.0)"<<endl;
  41.     cout<<"ptr = "<<ptr.getAddr()<<"---"<<ptr.getValue()<<'\n'<<endl;
  42.     point<int> pp = p;
  43.     cout<<"pp = p"<<endl;
  44.     cout<<"pp = "<<pp.getAddr()<<"---"<<pp.getValue()<<'\n'<<endl;
  45.     return 0;
  46. }
复制代码

----------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。

E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-az0eevfk.iyk --stdout=Microsoft-MIEngine-Out-51ky2brk.s3q --stderr=Microsoft-MIEngine-Error-adztnecr.jf5 --pid=Microsoft-MIEngine-Pid-kbpk5vvo.kqp "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
a = 0x61ff14---100

ptr默认数据
ptr = 0x61ff0c---0

ptr = a
ptr = 0x61ff0c---100

ptr.setValue(52.0)
ptr = 0x61ff0c---52

pp = p
pp = 0x61ff08---54



E:\Users\86184\Documents\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-12 11:05:42 | 显示全部楼层
什么功能啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-12 12:10:04 | 显示全部楼层
本帖最后由 bin554385863 于 2019-9-12 14:14 编辑


封装指针
  1. #include <iostream>
  2. template <typename t>
  3. class point
  4. {
  5. private:
  6.     t val;

  7. public:
  8.     point() : val(0) {}                   //默认构造
  9.     point(t var) : val(var) {}            //普通变量参数构造
  10.     point(t *ptr) : val(*ptr) {}          //指针参数构造
  11.     point(const point &p) : val(p.val) {} //复制构造

  12.     t *getAddr(int step = 0) //取地址.负值表示反向移动
  13.     {
  14.         t *tmp = &val;
  15.         return (tmp + step);
  16.     }
  17.     t getValue(int step = 0) //取值,负值表示反向移动
  18.     {
  19.         t *tmp = &val;
  20.         return *(tmp + step);
  21.     }
  22.     void setValue(t var, int step = 0) //设值
  23.     {
  24.         *(&val + step) = var;
  25.     }
  26.     ~point() {}
  27. };
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-12 18:08:20 | 显示全部楼层
本帖最后由 bin554385863 于 2019-9-12 18:23 编辑

修改版
  1. #include <iostream>
  2. template <typename t>
  3. class ptr
  4. {
  5. private:
  6.     t val;
  7.     int Step;

  8. public:
  9.     ptr() : val(0), Step(0) {}                 //默认构造
  10.     ptr(t var) : val(var), Step(0) {}          //普通变量参数构造
  11.     ptr(t *pt) : val(*pt), Step(0) {}          //指针参数构造
  12.     ptr(const ptr &p) : val(p.val), Step(0) {} //复制构造

  13.     void setPtrOffset(int step = 0) //指针偏移值,-负值反向
  14.     {
  15.         Step = step;
  16.     }
  17.     void setValue(t var) //设值
  18.     {
  19.         *(&val + Step) = var;
  20.     }
  21.     t *getPtrValue() //取地址.负值反向
  22.     {
  23.         return (&val + Step);
  24.     }
  25.     t getValue() //取值,负值反向
  26.     {
  27.         return *(&val + Step);
  28.     }
  29.     int getPtrOffset(ptr p) //取得偏移值
  30.     {
  31.         return p.getPtrValue() - this->getPtrValue();
  32.     }
  33.     ~ptr() {}
  34. };

  35. int main(int argc, char const *argv[])
  36. {
  37.     using namespace std;
  38.     int a = 100, b = 54;
  39.     int *p;
  40.     p = &b;
  41.     cout << "a = " << &a << "---" << a << '\n'
  42.          << endl;
  43.     ptr<int> pt;
  44.     cout << "pt默认数据" << endl;
  45.     cout << "pt = " << pt.getPtrValue() << "---" << pt.getValue() << '\n'
  46.          << endl;
  47.     pt = a;
  48.     cout << "pt = a" << endl;
  49.     cout << "pt = " << pt.getPtrValue() << "---" << pt.getValue() << '\n'
  50.          << endl;
  51.     pt.setValue(52.0);
  52.     cout << "pt.setValue(52.0)" << endl;
  53.     cout << "pt = " << pt.getPtrValue() << "---" << pt.getValue() << '\n'
  54.          << endl;
  55.     ptr<int> pp = p;
  56.     cout << "pp = p" << endl;
  57.     cout << "pp = " << pp.getPtrValue() << "---" << pp.getValue() << '\n'
  58.          << endl;
  59.     cout <<"pt.getValue() - pp.getValue() = " <<pt.getValue() - pp.getValue() << endl;
  60.     cout<<endl;
  61.     cout << "pt.getPtrOffset(pp) = "<<pt.getPtrOffset(pp)<<endl;
  62.     return 0;
  63. }
复制代码

---------------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。

E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-jroasrly.yxx --stdout=Microsoft-MIEngine-Out-lyphkmma.3mq --stderr=Microsoft-MIEngine-Error-4m0113jj.ua1 --pid=Microsoft-MIEngine-Pid-afflexgm.020 "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
a = 0x61fef8---100

pt默认数据
pt = 0x61feec---0

pt = a
pt = 0x61feec---100

pt.setValue(52.0)
pt = 0x61feec---52

pp = p
pp = 0x61fee4---54

pt.getValue() - pp.getValue() = -2

pt.getPtrOffset(pp) = 6


E:\Users\86184\Documents\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 14:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表