模板函数例子以及友元函数滥用的问题

使用模板函数实现一个复数类,熟悉模板函数的使用:

(1)在类的内部实现

#include <iostream>
using namespace std;

template <typename T>
class Complex
{
	friend ostream & operator<< <T>(ostream &out, Complex c)
	{
		out << c.a << "+" << c.b << endl;
		return out;
	}
public:
	Complex(T a, T b)
	{
		this->a = a;
		this->b = b;
	}

	Complex operator+ (Complex &c)
	{
		Complex tem(a+c.a,b+c.b);
		return tem;
	}
private:
	T a;
	T b;

};


int main()
{
	Complex<int> c1(1, 2), c2(3, 4);
	Complex<int> c3 = c1 + c2;

	cout << c3 << endl;

	system("pause");
	return 0;
}

(2)在类的外部实现(在一个.cpp文件)

#include <iostream>
using namespace std;

template <typename T>
class Complex
{
	friend ostream & operator<< <T> (ostream &out, Complex &c);//注意此处<T>位置,若无<T>则报错
public:
	Complex(T a, T b);

	Complex operator+ (Complex &c);
	Complex operator- (Complex &c);

private:
	T a;
	T b;
};

template <typename T>
Complex<T>::Complex(T a, T b)
{
	this->a = a;
	this->b = b;
}

template <typename T>
Complex<T> Complex<T>::operator+ (Complex<T> &c)
{
	Complex tem(a + c.a, b + c.b);
	return tem;
}

template <typename T>
Complex<T> Complex<T>::operator- (Complex<T> &c)
{
	Complex tem(a-c.a,b-c.b);
	return tem;
}

template <typename T>
ostream & operator<< (ostream &out, Complex<T> &c)
{
	out << c.a << " + " << c.b;
	return out;
}

int main()
{
	Complex<int> c1(1,2);
	Complex<int> c2(3, 4);

	Complex<int> c3 = c1 + c2;
	Complex<int> c4 = c2 - c1;

	cout << c3 << endl;
	cout << c4 << endl;

	
	system("pause");


	return 0;
}

需特别注意重载<<操作符时用到了友元函数,此时必须在operator<<后加上<T>,否则无法编译通过

不可以滥用友元函数(不是使用友元函数重载<< >>的情况),否则会产生错误,此时必须在类的前面添加类的前置声明以及函数的前置声明:

template <typename T>
class Complex;
template <typename T>
Complex<T> mySub(Complex<T>&c1, Complex<T>&c2);

同时在类的内部的声明必须写成:

friend Complex<T> mySub<T>(Complex<T>&c1, Complex<T>&c2);

友元函数的实现写成:

template <typename T>
Complex<T> mySub<T>(Complex<T>&c1, Complex<T>&c2)
{
	Complex<T> tem(c1.a - c2.a,c1.b - c2.b);
	return tem;
}

友元函数的调用写成:

Complex<int> c5 = mySub<int>(c2, c1);

(3)所有函数都写在类的外部(.h和.cpp分开)

.h文件

#pragma once
#include <iostream>
using namespace std;

template <typename T>
class Complex
{
	friend ostream & operator<< <T> (ostream &out, Complex &c);//注意此处<T>位置,若无<T>则报错
public:
	Complex(T a, T b);

	Complex operator+ (Complex &c);
	Complex operator- (Complex &c);

private:
	T a;
	T b;
};

.cpp文件:

#include "Complex.h"

template <typename T>
Complex<T>::Complex(T a, T b)
{
	this->a = a;
	this->b = b;
}

template <typename T>
Complex<T> Complex<T>::operator+ (Complex<T> &c)
{
	Complex tem(a + c.a, b + c.b);
	return tem;
}

template <typename T>
Complex<T> Complex<T>::operator- (Complex<T> &c)
{
	Complex tem(a - c.a, b - c.b);
	return tem;
}

template <typename T>
ostream & operator<< (ostream &out, Complex<T> &c)
{
	out << c.a << " + " << c.b;
	return out;
}

main文件:

#include "Complex.cpp"//注意此处引入的是.cpp文件

int main()
{
	Complex<int> c1(1,2);
	Complex<int> c2(3, 4);
	Complex<int> c3 = c1 + c2;

	cout << c3 << endl;
	system("pause");

	return 0;
}

注意引入文件时引入的是.cpp文件,否则会报找不到函数

(4)模板函数中的静态变量

不同类型各自使用自己的静态变量

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值