NDK14_C++基础:模板方法,模板类

NDK开发与模板编程
本文深入探讨了NDK开发的技巧与应用,重点讲解了模板编程在C++中的运用,包括函数模板和模板类的定义与使用,通过具体实例展示了如何通过模板实现类型无关的代码,提高代码复用性和效率。

NDK开发汇总

模板是泛型编程的基础

一 模板函数

函数模板能够用来创建一个通用的函数。以支持多种不同的形參。避免重载函数的函数体反复设计。

template <typename T> 
T max(T a,T b)
{
	// 函数的主体
	return  a > b ? a : b;
}
//代替了
int max(int a,int b)
int max(float a,float b)

跟java中的泛型类型

  1. 有一个int值交换需求
void myswap(int& a, int& b) {
	int temp = 0;
	temp = a;
	a = b;
	b = temp; 
}

void main() {
	int a = 999;
	int b = 666;

	myswap(a, b);

	cout << "a = " << a << " b = " << b << endl;

	system("puase");
}

运行结果:a = 666 b = 999

  1. 由int类型改成交换char 类型
void myswap(char& a, char& b) {
	char temp = 0;
	temp = a;
	a = b;
	b = temp; 
}

void main() {
	char a = 'a';
	char b = 'h';

	myswap(a, b);

	cout << "a = " << a << " b = " << b << endl;

	system("pause");
}

结果:a = h b = a
3. c中可以利用模板函数实现

template <typename T>
void myswap(T& a, T& b) {
	T temp = 0;
	temp = a;
	a = b;
	b = temp;
}

void main() {
	int a = 999;
	int b = 666;

	myswap(a, b);
	cout << "a = " << a << " b = " << b << endl;

	char c = 'a';
	char d = 'h';

	myswap(c, d);
	cout << "c = " << c << " d = " << d << endl;

	system("pause");
}

运行结果:
a = 666 b = 999
c = h d = a

二 模板类(泛型类)

为类定义一种模式。使得类中的某些数据成员、默写成员函数的參数、某些成员函数的返回值,能够取随意类型

常见的 容器比如 向量 vector 或 vector 就是模板类

template <class T>
class A
{
public:
	A(T a) {
		this->a = a;
	}
	~A() {

	}

protected:
	T a;
};


template<class T>
class C :public A<T>
{
public:
	C(T c,T a):A<T>(a) {
		this->c = c;
	}
	
	void tell() {
		cout << "a:" << a << " c:" << c << endl;
	}

private:
	T c;
};

三 Demo

Demo

::ndk::ScopedAStatus BpCameraService::addListener(const std::shared_ptr<::aidl::android::frameworks::cameraservice::service::ICameraServiceListener>& in_listener, std::vector<::aidl::android::frameworks::cameraservice::service::CameraStatusAndId>* _aidl_return) { binder_status_t _aidl_ret_status = STATUS_OK; ::ndk::ScopedAStatus _aidl_status; ::ndk::ScopedAParcel _aidl_in; ::ndk::ScopedAParcel _aidl_out; _aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR()); if (_aidl_ret_status != STATUS_OK) goto _aidl_error; _aidl_ret_status = ::ndk::AParcel_writeData(_aidl_in.get(), in_listener); if (_aidl_ret_status != STATUS_OK) goto _aidl_error; _aidl_ret_status = AIBinder_transact( asBinder().get(), (FIRST_CALL_TRANSACTION + 0 /*addListener*/), _aidl_in.getR(), _aidl_out.getR(), 0 #ifdef BINDER_STABILITY_SUPPORT | FLAG_PRIVATE_LOCAL #endif // BINDER_STABILITY_SUPPORT ); if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && ICameraService::getDefaultImpl()) { _aidl_status = ICameraService::getDefaultImpl()->addListener(in_listener, _aidl_return); goto _aidl_status_return; } if (_aidl_ret_status != STATUS_OK) goto _aidl_error; _aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR()); if (_aidl_ret_status != STATUS_OK) goto _aidl_error; if (!AStatus_isOk(_aidl_status.get())) goto _aidl_status_return; _aidl_ret_status = ::ndk::AParcel_readData(_aidl_out.get(), _aidl_return); if (_aidl_ret_status != STATUS_OK) goto _aidl_error; _aidl_error: _aidl_status.set(AStatus_fromStatus(_aidl_ret_status)); _aidl_status_return: return _aidl_status; }
最新发布
08-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值