第四周 :C++面向对象(下)之一

1.conversion function 转换函数

转换函数,用作类型转换,编译器可以自动调用,当然也可以显示调用,C风格的(type)value,C++风格的static_castvalue 都是可以的
需要注意的是如果类已经有转换函数,就不需要再写这个类与要转换的类的运算符重载了,因为会出现二义性,例如代码注释的部分

代码:

// 1.conversion function 转换函数
#include <iostream>
#include <stdio.h>
using namespace std;

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

    operator double() const {
        printf("-------operator double() const-----\n");
        return (double)(m_numerator/m_denominator);
    }
    //friend double operator +(double left, Fraction tmp);
private:
    int m_numerator;
    int m_denominator;
};

/*
double operator + (double left, Fraction tmp)
{
    return left + tmp.m_numerator/tmp.m_denominator;
}
*/
int main(int argc, char *argv[])
{
    Fraction f(3,5);

    double d = 4 + f;
    double r = (double)f;
    double s = static_cast<double>(f);

    return 0;
}

/*
operator double() const
operator double() const
operator double() const
*/

2.non-explicit-one-argument ctor 没有指定明确调用的单一实参的构造函数

代码:

// 2.non-explicit-one-argument ctor, 没有指定明确调用的单一实参的构造函数
#include <iostream>
#include <stdio.h>

class Fraction
{
public:
    Fraction(int num, int den=1)
        :m_numerator(num), m_denominator(den)   {
        printf("num=%d, den=%d\n", num, den);
    }

    Fraction operator+(const Fraction &f)   {
        printf("Fraction operator+(const Fraction &f)\n");
        //TODO
        return Fraction(0); //添0是为了编译通过 并不是正确的结果
    }

private:
    int m_numerator;
    int m_denominator;
};

int main(int argc, char *argv[])
{
    Fraction f(3,5);
    Fraction d = f + 4 ; // 调用 non-explicit-one-argument ctor将4转换为Fraction(4,1),然后调用 operation+
    return 0;
}

/*
num=3, den=5
num=4, den=1
Fraction operator+(const Fraction &f)
num=0, den=1
*/

Fraction的构造函数有两个参数,但是有一个是默认参数,所以可以等同是单一参数的构造函数,
编译器在执行f+4的时候,会先想办法
首先匹配到重载的
Fraction operator+(const Fraction &f)
但是只适用于Fraction类之间运算,然后编译器就试着把4转换位Fraction类,
于是发现Fraction有一个non-explicit-one-argument ctor,并且参数刚好是int类型,所以就进行了隐示转换,把4转换成Fraction类,
之后在调用Fraction operator+(const Fraction &f),返回一个新的Fraction.
能够把4转换成Fraction类,是因为Fraction的构造函数没有声明explicit,即该构造函数只能显示调用,不能进行隐示调用

     operator double() const {
         return (double)(m_numerator/m_denominator);
     }

此时添加Fraction转 double 的转化函数,编译器就不会通过了。
代码:

#include <iostream>
#include <stdio.h>

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

    operator double() const {
         return (double)(m_numerator/m_denominator);
     }

    Fraction operator+(const Fraction &f)   {
        printf("Fraction operator+(const Fraction &f)\n");
        //TODO
        return Fraction(0); //添0是为了编译通过 并不是正确的结果
    }

private:
    int m_numerator;
    int m_denominator;
};

int main(int argc, char *argv[])
{
    Fraction f(3,5);
    Fraction d = f + 4 ; // 调用 non-explicit-one-argument ctor将4转换为Fraction(4,1),然后调用 operation+
    return 0;
}

因为f+4既可以使用操作符重载,也可以使用隐式类型转换,具有二义性ambiguous

如果此时把Fraction的构造声明为explicit,还会报错:cannot convert from ‘double’ to ‘class Fraction因为声明为explicit的构造函数不能在隐式转换中使用

3.pointer-like classes 行为像指针的类

 1. 典型应用 1:**智能指针**

智能指针的基本功能应该重载 * 和 ->两个操作符,和相应的构造析构函数

如图:
这里写图片描述
下面是示例代码:

#include <iostream>
struct Foo
{
    void method()
    {
        printf("---------------------\n");
    }
    int value;
};

template <class T>
class shared_ptr
{
public:
    T& operator*() const {
        return *px;
    }

    T* operator-> () const {
        return px;
    }

    shared_ptr(T* p) : px(p) {}

    ~shared_ptr(){ delete px; }
private:
    T*      px;
};

void func()
{
    shared_ptr<Foo> pFoo(new Foo);

    Foo f = *pFoo;
    pFoo->method();
}

int main(int argc, char *argv[])
{
    func();
    return 0;
}
 2. 典型应用2: **迭代器**

迭代器提供对一个容器中的对象的访问方法,并且定义了容器中对象的范围 自我感觉不能很好的用文字表述出来,所以直接把胶片放上,便于以后回忆


这里写代码片

这里写图片描述

这里写图片描述

1迭代器和智能指针一样都要重载 * 和 -> 这两个操作符,但是内部实现却是不一样的。
下图说明智能指针和迭代器重载操作的不同:

智能指针重载操作符:
这里写图片描述

迭代器重载操作符:
这里写图片描述

显然两者实现的功能不一样,因此在重载操作符内部代码也不尽相同。

4. function-like classes 像函数的类仿函数

C/C++ 之 typedef的用法

仿函数就是类重载了() 操作符
下列代码中简单的实现了C++的pair, 其中Select1st, Select2nd就是仿函数
写的过程中熟悉了pair的first_type,second_type 还有vaue_type的用法

#include <iostream>

template <class T1,class T2>
struct pair {
    typedef T1 first_type;          // 声明 T1 泛型类型的 别名为first_type 以后可以用first_type代表T1类型
    typedef T2 second_type;

    typedef pair<T1,T2> value_type; // 声明 pair<T1,T2> 泛型类型的 别名为value_type 以后可以用value_type代表pair<T1,T2>类型
    pair()
        : first(T1()),second(T2())
    {}
    pair(const T1& a, const T2& b)
        : first(a),second(b)
    {}

    T1 first;
    T2 second;
};

template <class T>
struct identity  {
    const T&
    operator() (const T& x) const { return x;}
};

template <class Pair>
struct Select1st{
    const typename Pair::first_type &
    operator() (const Pair& x) const
    {
        return x.first;
    }
};

template <class Pair>
struct Select2nd{
    const typename Pair::second_type &
        operator() (const Pair& x) const
    {
        return x.second;
    }
};

int main(int argc, char *argv[])
{
    pair<int,double> p(10,20.3);

    Select1st<pair<int,double> > s1;
    Select2nd<pair<int,double> > s2;

    std::cout << "first : " << s1(p) << std::endl;
    std::cout << "second : " << s2(p) << std::endl;

    return 0;
}

5. template 模板

与class template不同,function template在使用的时候并不需要指定类型,而是编译器会对function template进行实参推导argument deduction, 确定类型

 1. 典型应用1: function template 函数模板
#include <iostream>

template <class T>
inline 
const T& Min(const T& a, const T& b)
{
    return b < a ? b : a;
}

int main(int argc, char *argv[])
{
    int a = 10, b = 20;
    std::cout << Min(a,b) << std::endl;

    return 0;
}
 2. 典型应用2: member template 成员模板留坑

简单点来说就是类成员某些参数需要模板
下面以智能指针举例
但是我没有想明白在这个例子中这么做的必要性,因为就算没有这个模板的拷贝构造,这个也是可以的,因为父类指针可以指向子类对象, 留坑,等想到更好例子在进行更新

#include <iostream>

template <typename T>
class shared_ptr
{
public:
    T& operator*() const {
        return *_px;
    }

    T* operator-> () const {
        return _px;
    }

    explicit shared_ptr(T* p) : _px(p) { std::cout << "shared_ptr(T* p)" <<std::endl;}

    template <typename _Tp1>
    explicit shared_ptr(_Tp1* p)
    :_px(p)
    {std::cout << "shared_ptr(_Tp1* p)" <<std::endl;    }

    ~shared_ptr(){ delete _px; }
private:
    T* _px;
};

class A
{
    //..
};

class B : public A 
{
    //..
};

int main(int argc, char *argv[])
{
    B * pa = new B;
    shared_ptr<A> pSA(new B);

    return 0;
}

6.Specialization 模板特化

参考链接:

就是在一个模板类里面,为某几种已知的类进行单独处理
下面以函数模板举例,对int进行模板特化

/* 模板的特化 */

#include <iostream>

/* 定义一个泛型模板 */
template <class T>
inline
const T& Min(const T& a, const T& b)
{
    std::cout << "const T& Min(const T& a, const T& b)" <<std::endl;
    return b < a ? b : a;
}
/* 将泛型模板特化为int类型 */
template <>
inline
const int& Min<int>(const int& a, const int& b)
{
    std::cout << "const int& Min<int>(const int& a, const int& b)" <<std::endl;
    return b < a ? b : a;
}

int main(int argc, char *argv[])
{
    std::cout << Min(10,20) << std::endl;
    std::cout << Min(100.2, 200.3) << std::endl;;

    return 0;
}
/*
const int& Min<int>(const int& a, const int& b)
10
const T& Min(const T& a, const T& b)
100.2
*/ 

7.partial Specialization 模板偏特化局部特化

参考链接:模板的全特化与偏特化
偏特化分为两种:个数上的偏特化和范围上的偏特化

 1. 个数的偏特化

部分模板参数偏特化,在VC6上会编译失败,Qt的C++11是可以的

template <class T1, class T2>
struct MyPair
{
    MyPair(const T1& a, const T2& b)
        :first(a), second(b)
    {}
    T1 first;
    T2 second;
};

template <class T2>
struct MyPair<bool>
{
    MyPair(const bool& a, const T2& b)
        :first(a), second(b)
    {}
    bool first;
    T2 second;
};
 2. 范围上的偏

这个主要解决模板指针进行模板的方式,在VC6上会编译失败,Qt的C++11是可以的

template <typename T1>
class C
{
};

template<typename T1>
class C<T1*>
{
};

8.template template parameter 模板模板参数

一个模板定义的template中不是自定义或基础数据类型,而是另一个模板
例如下面例子中,XCLs模板中,有两 typename
  其中一个是T的模板,基础类型或者自定义类型
  另一个是模板类,这个模板类的只有一个 typename ,并且他的T和XCLs的第一个 typename 类型一样
下列代码中给出了实例化这个模板类的展开样子,更有助益理解

#include <iostream>
using namespace std;

template <typename T, template <typename T> class TempClass>
class XCLs
{
private:
    TempClass<T>    c;
};

template <typename T>
using Lst = list<T, allocator<T>>; 
// 因为list其实有多个模板参数,但是XCLs的第二个模板参数只接受只有一个模板参数的模板类,所以通过 这种方式把list其他参数固化

template <typename T>
class A{
    T a;
};

int main(int argc, char *argv[])
{
    XCLs<string, Lst> mylist;
    XCLs<string, A> myA;
    return 0;
}

/* XCLs<string, Lst> mylist ---> 展开

template <string, template <string> class list>
class XCLs
{
private:
    list<string>    c;
};
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值