模板学习笔记2

传智播客视频——day9


项目开发中有三种情况:

  1. 所有类模板函数都写在类的内部
  2. 所有类模板函数都写在类外部,但在同一个.cpp中
  3. 所有类模板函数都写在类外部,但写在不同.cpp和.h中

    shift+delete:删除一行

类模板知识梳理

1.所有函数都写在类的内部:

class Complex
{
    friend ostream & operator<< (ostream &out, Complex & c3);//说明这个operator的函数可以方位Complex类里私有变量
private:
    int a; 
    int b;
public:
    Complex(int a, int b)//构造函数
    {
        this->a = a;
        this->b = b;
    }
    void printcom()
    {
        cout << "a: " << a << " b: " << b << endl;
    }
    Complex operator+(Complex &c2)//c2是右操作数,左操作数没写时,默认现在所在的这个类
    {
        Complex temp(a + c2.a, b + c2.b);
        return temp;
    }
};
- - - - - - - - - - - - - - - - - - - - - - - -
ostream & operator<< (ostream &out, Complex &c3)
{
    out << "a: " << c3.a << " b: " << c3.b << endl;
    return out;
}
---------------------------------------------------------------
int main()
{
    Complex  c1(1, 2);
    Complex  c2(3, 4);
    Complex c3 = c1 + c2;//用到+操作符重载
    //Complex operator+(Complex &c2);
    cout << c1 << endl;
    //要用到<<操作符的重载,cout是左操作数,c1是右操作数,返回引用
    //ostream & operator<< (ostream &out, Complex&);
    return 0;
}

加入函数模板对比:

template <typename T>
class Complex
{
    friend ostream & operator<< (ostream &out, Complex &c3)
    {
        out << c3.a << " + " << c3.b <<"i"<< endl;
        return out;
    }
    private:
        T  a; 
        T  b;
    public:
        Complex(T  a, T  b)//构造函数
        {
            this->a = a;
            this->b = b;
        }
        void printcom()
        {
            cout << "a: " << a << " b: " << b << endl;
        }
        Complex operator+(Complex &c2)//c2是右操作数,左操作数没写时,默认现在所在的这个类
        {
            Complex temp(a + c2.a, b + c2.b);
            return temp;
        }
    };
---------------------------------------------------------------
int main()
{
    Complex<int>  c1(1, 2);//编译器要分配内存,所以要指定参数列表
    Complex<int>  c2(3, 4);
    Complex<int>  c3 = c1 + c2;//用到+操作符重载
    cout << c1 << endl;//用到<<重载
//友元函数在类的外部,编译不能通过;如果移到类内部就可以通过
    return 0;
}

运算符重载的正规写法
重载<<,>>只能用友元函数,其他运算符重载 都要写成成员函数,不要滥用友元函数。


2.类模板函数都写在类外部,但在同一个.cpp中

template <typename T>
class Complex
{
    friend ostream & operator<< <T>(ostream &out, Complex &c3);//本质是模板的二次编译,所以要加<T>
    public:
        Complex(T  a, T  b)//构造函数,深拷贝
        void printcom();
        Complex operator+(Complex &c2);
    private:
        T  a; 
        T  b;
};
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
template <typename T>
void Complex<T>::Complex(T  a, T  b)//构造函数,深拷贝
{
    this->a = a;
    this->b = b;
}

template <typename T>
void Complex<T>:: printcom()
{
    cout << "a: " << a << " b: " << b << endl;
}
//成员函数实现 实现 + 运算符重载
template <typename T>
Complex<T> Complex<T>::operator+(Complex<T> &c2)//函数参数,返回值需要具体类型化
{
    Complex<T> temp(a + c2.a, b + c2.b);//这里面的T可加可不加,因为外面返回类型写了
    return temp;
}
//友元函数 实现 << 运算符重载
template <typename T>
ostream & operator<< (ostream &out, Complex<T> &c3)//友元函数是全局函数,不属于这个类的,不需要加Complex<T>::
{
    out << c3.a << " + " << c3.b <<"i"<< endl;
    return out;
}

---------------------------------------------------------------
int main()
{
    Complex<int>  c1(1, 2);//编译器要分配内存,所以要指定参数列表
    Complex<int>  c2(3, 4);
    Complex<int>  c3 = c1 + c2;//用到+操作符重载
    cout << c1 << endl;//用到<<重载
//友元函数在类的外部,编译不能通过;如果移到类内部就可以通过
    return 0;
}

2.类模板函数都写在类外部,.cpp和.h分开
把传统意义上的.cpp和.h改写成.hpp和.h。

类模板中static关键字

对于不同的类型,产生不同的static变量,他们之间没联系。

template<typename T>
class A
{
public:
    static T m_a;
private:
};
template <typename T>
T A<T>::m_a=0;//static变量在类外面初始化
---------------------------------------
main:
A<int> a1,a2,a3;
a1.m_a=10;
a2.m_a++;
a3.m_a++;
A<char> b1,b2;
b1.m_a='a';
b2.m_a++;
cout<<A<int>::m_a<<endl;//12
cout<<A<int>::m_a<<endl;//b
//m_a每一种类型的类,使用自己的静态变量 

类模板的应用

编写MyVector类,使用类模板,重载[],=运算符,拷贝构造。

MyVector.h

#include<isotream.h>
using namespace std;
template<typename T>
class MyVector
{
    friend ostream & operator<< <T>(ostream &out,const MyVector &obj);//友元函数重载<<
public:
    MyVector(int size=0);//构造函数
    MyVector(const MyVector &obj);//拷贝构造函数
    ~MyVector();
public:
    MyVector & operator= (const MyVector &obj);
    T& operator[] (int index);//函数返回值当左值要返回引用
private:
    T *m_space;
    int m_len;
}

MyVector.cpp

#include<isotream.h>
#include"MyVector.h"
using namespace std;

template<typename T>        //MyVector<int> a(10);
MyVector<T>::MyVector(int size=0)
{
    m_space=new T[size];//int *p=new int[]
    m_len=size; 
}

template<typename T>        //MyVector<int> a(b);
MyVector<T>::MyVector(const MyVector &obj)
{
    //根据obj的大小分配内存,在一个个拷贝数据
    m_len=obj.m_len;
    m_space=new T[m_len];
    for(int i=0;i<m_len;i++)
    {
        m_space[i]=obj[i];
    }
}

template<typename T>
MyVector<T>::~MyVector()
{
    if(m_space!=NULL)
    {
        delete [] m_space;
        m_space=NULL;
        m_len=0;    
    }   
}

template<typename T>        //a3=a2=a1;拷贝构造函数
MyVector<T> &  MyVector<T>:: operator= (const MyVector<T> &obj)
{
    //先把a2的旧的内存释放掉
    if(m_space!=NULL)
    {
        delete [] m_space;
        m_space=NULL;
        m_len=0;
    }
    //根据a1分配内存
    m_len=obj.m_len;
    m_space=new T[m_len];
    for(int i=0;i<m_len;i++)
    {
        m_space[i]=obj.m_space[i];
    }
    return *this;//a2=a1,返回a2的自身
}

template<typename T>
T& MyVector<T>::operator[] (int index)
{
    return m_space[index];
}

template<typename T>
ostream & operator<< (ostream &out,const MyVector<T> &obj)
{
    for(int i=0;i<obj.m_len;i++)
        out<<obj.m_space[i]<<" ";
    cout<<endl;
    return out;
}

test.c

MyVector<int> a(10);
for(int i=0;j<getlen();i++)
{
    a[i]=i++;
}
cout<<a<<endl;//<<重载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值