> 文章列表 > 类模板的三种表达方式

类模板的三种表达方式

类模板的三种表达方式

一:所有的类模板函数写在类的内部

template <typename T>
class A {
public:A(T a=0) {this->a = a;}T& getA() {return this->a;}A operator+(const A& other) {A temp;//要求A的构造函数要有默认值temp.a = this->a + other.a;return temp;}
private:T a;
};

二:所有的类模板函数写在类的外部,同一个.cpp文件

template <typename T>
class A {
public:A(T a = 0);T& getA();A operator+(const A& other);
private:T a;
};template <typename T>
A<T>::A(T a ) {this->a = a;
}template <typename T>
T& A<T>::getA() {return this->a;
}template <typename T>
A<T> A<T>::operator+(const A<T>& other) {A temp;//类的内部可以显示声明也可以不显示temp.a = this->a + other.a;return temp;
}

注意:

三:所有的类模板函数写在类的外部,在不同的.h和.cpp文件中

方式一:main函数处在A.cpp文件中

A.h

#pragma once
template <typename T>
class A {
public:A(T a = 0);T& getA();A operator+(const A& other);
private:T a;
};

A.cpp 

#include "A.h"template <typename T>
A<T>::A(T a) {this->a = a;
}template <typename T>
T& A<T>::getA() {return this->a;
}template <typename T>
A<T> A<T>::operator+(const A<T>& other) {A temp;//类的内部可以显示声明也可以不显示temp.a = this->a + other.a;return temp;
}int main(void) {A<int> a(100);A<int> b(200);cout << a.getA() << endl;cout << b.getA() << endl;A<int> temp = a + b;cout << temp.getA() << endl;return 0;
}

方式二: main函数不在A.cpp文件中

A.h

#pragma once
#include<iostream>
using namespace std;template <typename T>
class A {
public:A(T a = 0);T& getA();A operator+(const A& other);
private:T a;
};

 A.cpp

#include "A.h"template <typename T>
A<T>::A(T a) {this->a = a;
}template <typename T>
T& A<T>::getA() {return this->a;
}template <typename T>
A<T> A<T>::operator+(const A<T>& other) {A temp;//类的内部可以显示声明也可以不显示temp.a = this->a + other.a;return temp;
}

main.cpp   特别注意:此时不可包含模板类的头文件,需要包含模板类的.cpp函数实现文件,否则编译器报错

#include<iostream>
#include"A.cpp"
using namespace std;int main(void) {A<int> a(100);A<int> b(200);cout << a.getA() << endl;cout << b.getA() << endl;A<int> temp = a + b;cout << temp.getA() << endl;return 0;
}

提示:根据业内不成文的规定,模板类的具体实现文件一般命名为.hpp,此时主函数文件中需要同时包含此.hpp文件,这种命名方式可读性更好.

四:特殊情况,友元函数

#include<iostream>
using namespace std;template <typename T>
class A {
public:A(T a = 0);T& getA();A operator+(const A& other);template <typename T> //必须写成该模式friend A<T> addA(const A<T>& a, const A<T>& b);
private:T a;
};template <typename T>
A<T> addA(const A<T>& a, const A<T>& b) {A<T>temp;//友元函数不属于类的内部,所以必须声明temp.a = a.a + b.a;return temp;
}template <typename T>
A<T>::A(T a ) {this->a = a;
}template <typename T>
T& A<T>::getA() {return this->a;
}template <typename T>
A<T> A<T>::operator+(const A<T>& other) {A temp;//类的内部可以显示声明也可以不显示temp.a = this->a + other.a;return temp;
}int main(void) {A<int> a(100);A<int> b(200);A<int> temp = addA<int>(a, b);//300cout << temp.getA() << endl;return 0;
}

友元函数在模板类中的使用总结:

(1)类的内部声明必须写成如下例子的形式:

template <typename T>
friend A<T> addA(const A<T>& a, const A<T>& b);

(2)友元函数的实现必须写成:

template <typename T>
A<T> addA(const A<T>& a, const A<T>& b) {A<T>temp;//友元函数不属于类的内部,所以必须声明temp.a = a.a + b.a;return temp;
}

(3)友元函数的调用必须写成:

A<int> a(100);
A<int> b(200);
A<int> temp = addA<int>(a, b);//300