bin554385863 发表于 2019-9-12 02:01:52

C++无聊瞎写的

本帖最后由 bin554385863 于 2019-9-12 02:03 编辑

#include <iostream>
template <typename t>
class point
{
private:
    t val;

public:
    point():val(0){}
    point(t var):val(var){}
    point(t* ptr):val(*ptr){}
    point(const point& p):val(p.val){}

    t *getAddr()
    {
      return &val;
    }
    t getValue()
    {
      return val;
    }
    void setValue(t var)
    {
      *(&val) = var;
    }
    ~point(){}
};

int main(int argc, char const *argv[])
{
    using namespace std;
    int a = 100, b = 54;
    int *p;
    p = &b;
    cout<<"a = "<<&a<<"---"<<a<<'\n'<<endl;
    point<int> ptr;
    cout<<"ptr默认数据"<<endl;
    cout<<"ptr = "<<ptr.getAddr()<<"---"<<ptr.getValue()<<'\n'<<endl;
    ptr = a;
    cout<<"ptr = a"<<endl;
    cout<<"ptr = "<<ptr.getAddr()<<"---"<<ptr.getValue()<<'\n'<<endl;
    ptr.setValue(52.0);
    cout<<"ptr.setValue(52.0)"<<endl;
    cout<<"ptr = "<<ptr.getAddr()<<"---"<<ptr.getValue()<<'\n'<<endl;
    point<int> pp = p;
    cout<<"pp = p"<<endl;
    cout<<"pp = "<<pp.getAddr()<<"---"<<pp.getValue()<<'\n'<<endl;
    return 0;
}

----------------------------------------------------------------------------------------------
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>

lh1996 发表于 2019-9-12 11:05:42

什么功能啊
{:5_108:}

bin554385863 发表于 2019-9-12 12:10:04

本帖最后由 bin554385863 于 2019-9-12 14:14 编辑

lh1996 发表于 2019-9-12 11:05
什么功能啊

封装指针
#include <iostream>
template <typename t>
class point
{
private:
    t val;

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

    t *getAddr(int step = 0) //取地址.负值表示反向移动
    {
      t *tmp = &val;
      return (tmp + step);
    }
    t getValue(int step = 0) //取值,负值表示反向移动
    {
      t *tmp = &val;
      return *(tmp + step);
    }
    void setValue(t var, int step = 0) //设值
    {
      *(&val + step) = var;
    }
    ~point() {}
};

bin554385863 发表于 2019-9-12 18:08:20

本帖最后由 bin554385863 于 2019-9-12 18:23 编辑

修改版
#include <iostream>
template <typename t>
class ptr
{
private:
    t val;
    int Step;

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

    void setPtrOffset(int step = 0) //指针偏移值,-负值反向
    {
      Step = step;
    }
    void setValue(t var) //设值
    {
      *(&val + Step) = var;
    }
    t *getPtrValue() //取地址.负值反向
    {
      return (&val + Step);
    }
    t getValue() //取值,负值反向
    {
      return *(&val + Step);
    }
    int getPtrOffset(ptr p) //取得偏移值
    {
      return p.getPtrValue() - this->getPtrValue();
    }
    ~ptr() {}
};

int main(int argc, char const *argv[])
{
    using namespace std;
    int a = 100, b = 54;
    int *p;
    p = &b;
    cout << "a = " << &a << "---" << a << '\n'
         << endl;
    ptr<int> pt;
    cout << "pt默认数据" << endl;
    cout << "pt = " << pt.getPtrValue() << "---" << pt.getValue() << '\n'
         << endl;
    pt = a;
    cout << "pt = a" << endl;
    cout << "pt = " << pt.getPtrValue() << "---" << pt.getValue() << '\n'
         << endl;
    pt.setValue(52.0);
    cout << "pt.setValue(52.0)" << endl;
    cout << "pt = " << pt.getPtrValue() << "---" << pt.getValue() << '\n'
         << endl;
    ptr<int> pp = p;
    cout << "pp = p" << endl;
    cout << "pp = " << pp.getPtrValue() << "---" << pp.getValue() << '\n'
         << endl;
    cout <<"pt.getValue() - pp.getValue() = " <<pt.getValue() - pp.getValue() << endl;
    cout<<endl;
    cout << "pt.getPtrOffset(pp) = "<<pt.getPtrOffset(pp)<<endl;
    return 0;
}

---------------------------------------------------------------------------------------------------
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>
页: [1]
查看完整版本: C++无聊瞎写的