美文网首页
GeekBand C++开发工程师第四周学习笔记

GeekBand C++开发工程师第四周学习笔记

作者: 茶香貂蝉 | 来源:发表于2016-03-24 11:54 被阅读0次

    4.1Conversion Function 转换函数

    class Fraction{

    public:

    operator double() const { //转换不可能改变类里的成员,通常加上const

    return (double)(m_numerator / m_denominator);//分数怎么转换为double

    }//把Fraction对象转换成double类型,double()不可有参数,返回类型不用写

    private:

    intm_numerator;//分子

    intm_denominator;//分母

    };

    Fraction f(3,5);

    double d=4+f;//调用operator double()将f转为0.6

    4.2non-explict it one arguement construct

    //可以把别的东西转换为这种东西

    class Fraction{

    public:

    Fraction(int num,int den=1):m_numerator(num),m_denominator(den) {  }

    operator double const {

    return (double)(m_numerator / m_denominator);

    }

    Fraction operator+(constFraction& f) {

    return

    Fraction(...);

    private:

    intm_numerator;//分子

    intm_denominator;//分母

    };

    Fraction f(3,5);

    Fractiond2=f+4;//调用non_explict ctor 将4转为Fraction,然后调用operator+

    template

    calss vector//模板的先特化,容器里的每个元素都是ool值

    {

    public:

    typedef __bit_reference reference;

    protected:

    reference operator[](size type n){//代表本来真正传回来的东西

    return *(begin() +diffenece_type(n));

    }

    struct __bit_reference

    {

    unsigned int* p;

    unsigned int mask;

    ...

    public:

    operator bool() const { return !(!(*p * mask));}

    };

    4.3.1

    pointer-like classes,关于智能指针

    template

    class shared_ptr{

    public:

    T& operator*() const

    {

    return *px;

    }

    T* operator->() const{

    return px;

    }

    shared_ptr(T* p):px(p) { }//构造函数,接受天然指针

    private:

    T* px;//指向T的指针,什么类型都接受

    long* pn;

    };

    lass Foo{

    ...

    void method(void) {...}

    };

    shared_ptr sp(new Foo);//ew得到一个指针,当成初值,塞到构造函数里面

    Foo f(*sp);

    sp->method();//通过智能指针的箭头,要去调用method()

    px->method();//通过这个对象调用这个函数

    箭头符号有一个特殊的行为:作用下去得到的结果,这个箭头符号会继续作用下去

    智能指针里头一定带着一个一般的指针,而智能指针一定要带*和->符号

    point-class 这种类型

    4.4 迭代器

    主要用来遍历容器

    template

    struct _list_iterator//链表的迭代器

    {

    typedef _list_iterator self;

    typedef Ptr pointrt;

    typedef Ref reference;

    typedef _list_node* linx_type;//指向结点的指针

    link_type node;

    bool operator == (const self& x) const {return node == x.node;}

    bool operator != (const self& x) const {return node != x.node;}

    reference operator*() const {return (*node}.data;}

    pointer operator-> const {return &(operator*());}

    self& operator++() {node = (link_type)((*node).next);return *this;}

    self operator++(int) {self tmp = *this; ++*this; return tmp;}

    self& operator--() {node = (link_type)((*node).prev;return *this;}

    self operator--(int) {self tmp = *this; --*this; return tmp;}

    };

    4.5 Function - like class 想一个函数

    template

    structidentity : public unart_function{

    const T& operator()(const T& x) const { return x;}//给了一个x就传一个x

    //所做出来的对象将接受小括号

    };

    template

    structselectst{//取出第一个

    const typename Pair::first_type& operator() (const Pair& x) const

    {

    return x.first;

    }

    };

    template

    structselect2nd : publicunart_function{//取出第二个

    const typename Pair::second_type& operator()(const Pair& x) const

    {

    return x.second;

    }

    };

    template

    structpair: publicunart_function{

    T1 first;

    T2 second;

    pair() : first(T1()),second(T2()) {}

    pair(const T1& a,const T2& b):first(a),second(b) { }

    };

    标准库中,反函数所使用的奇特的基类(大小为0,没有函数):

    unart_function//一个操作数的

    binary_function//两个操作数的

    4.6 class template 类模板和 fumction template 函数模板

    template//复数的类模板

    class complex{

    public:

    complex(T r=0,T i=0):re(r),im(i) {}

    complex & operator += (const complex&);

    T real() const {return re;}

    T imag() const {return im;}

    private:

    T re,im;

    friend complex& __doapl(complex*,const complex&);

    };

    {

    complex c1(2.5,1.5);

    complex c2(2,6);

    }

    函数模板:

    class stone{

    public:

    stone(int w,int h,int we):_w(w),_h(h),_weight(we) {}

    bool operator<<(const stone& rhs) const {

    return _weight < rhs._weight;

    }

    private:

    int _w,_h,_weight;

    };

    template

    inline const T& min(const T& a, const T& b)

    {  return b;}

    函数模板在使用的时候不必指明type,能够通过调用时传递的实参推出来类型

    member template 成员模板

    template

    struct pair{

    typedef T1 first_type;

    typedef T2 second_type;

    T1 first;

    T2 second;

    pair():first(T1()),second(T2()) {}

    pair(const T1 &a,const T2 &b)

    :first(a),second(b) {}

    template   //成员模板

    pair(const pait &p)//T1和T2类型确定了以后,U1和U2也能进行确定

    :first(p.first),second(p.second) {}

    }

    4.7模板特化 specialization

    template//key是一个符号,是什么都可以

    struct hash ();

    template<>//class T被绑定了,省略

    struct hash{

    size_t operator() (char x) const {return x;}//重载

    };

    template<>

    struct hash{

    size_t operator() (int x) const {return x;}

    };

    template<>

    struct hash{

    size_t operator() (long x) const {return x;}

    };

    cout<()(1000);//浅绿色表示的是一个临时对象,编译器进行到这里会去找特化,蓝色表示去启动这个函数,1000是参数,特化可以有无数个版本


    个人思考:运用智能指针的一些细节

    所谓智能智能,是“行为像指针”的对象,并提供指针没有的机能。例如std::auto_ptr和trl::shared_ptr可以用来在正确时机自动删除heap-based资源。就像STL容器的迭代器几乎总是智能指针。

    真实指针做得很好的一件事是:支持隐式转换。

    Derived class指针可以隐式转换为base class指针,“指向non-const对象”的指针可以转换为“指向const对象”......等等。

    下面是可能发生于三层继承体系的一些转换:

    class Top {...};

    class Middle: public Top {...};

    class Bottom:public Middle {...};

    Top *pt1 = new Middle;//将Middle*转换为Top*

    Top* pt2 = new Bottom;//将Botton*转换为Top*

    const Top* pct2 = pt1;//将Top*转换为const Top*

    但如果现在自定义的智能指针中模拟上述转换,就有点麻烦了。

    如果希望下面代码通过编译:

    template<typename T>

    class SmartPtr{

          public://智能指针通常以内置指针完成初始化

          explicit SmartPtr(T* realPtr);

          ...

    };

    SmartPtr<Top> pt1 = SmartPtr<Middle> (new Middle);

    SmartPtr<Top> pt2 = SmartPtr<Botton> (new Bottom);

    SmartPtr<const Top> pct2 = pt1;

    但是同一个template的不同具现体之间并不存在什么与生俱来的固有关系,所以编译器将SmartPtr<Middle>和SmartPtrBotton>视为完全不同的classes它们之间的关系并不像vector<float>和Weight之间的关系密切。

    所以为了获得我们所希望的SmartPtr classes之间的转换能力,必须将它们明确的编写出来。

    在上述的指针指针的实例中,每一个语句创建了一个新式智能指针对象,所以现在更应该关注如何编写智能指针的构造函数,使其行为能够满足我们的转型需要。

    但有一个问题是,我们可能无法写出我们所需要的所有构造函数。

    在上述的继承体系中,我们根据一个SmartPtr<Middle>或一个SmartPtr<Bottom>构造出一个SmartPtr<Top>, 如果这个继承体系未来有所扩充,SmartPtr<Top>对象又必须能够根据其他智能指针构造自己。


    相关文章

      网友评论

          本文标题:GeekBand C++开发工程师第四周学习笔记

          本文链接:https://www.haomeiwen.com/subject/iidrlttx.html