类模板的三种表达方式

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

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);//300
	cout << 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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值