网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android系统之仿sp智能指针实例-创新互联

目录

公司主营业务:网站建设、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出索县免费做网站回馈大家。

1.仿sp实例 

2.其实本质是普通类继承模板类的一个例子。参考之前写过的一篇,帮助理解。

3.程序例子01

4.程序例子02


1.仿sp实例 

为了便于理解Android智能指针模板类make()创建的过程,写了一个仿智能指针的一个例子,方便初学者理解。

2.其实本质是普通类继承模板类的一个例子。参考之前写过的一篇,帮助理解。

C++普通类继承模版类demo_Android系统攻城狮的博客-博客

3.程序例子01
#includeusing namespace std;

templateclass sp {
public:
	templatestatic inline spmake(Args&&... args);

	void test_sp(){
		printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
	}
	T* m_ptr;
};

templatetemplatespsp::make(Args&&... args) {
	T* t = new T(std::forward(args)...);
	spresult;
	result.m_ptr = t;
	return result;
}

class BpBinder : public sp{
	//friend class sp;

public:
	BpBinder(int id, char *name){
		printf("id = %d, name = %s\n",id,name);
	}

	void test_BpBinder(){
		printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
	}
	int num;
};

int main(){
	int id = 44;
	char buf[] = "ServiceManager";

	//1.普通类继承模板类,在模板类中实例化普通类,并返回普通类的实例对象
	spbc = sp::make(id, buf);
	//bc.test_BpBinder(); //failed
	bc.test_sp();
	bc.m_ptr->test_sp();	
	
	//2.第二种写法
	spbb;
	bb.make(id, buf);

	//3.不借用模板类sp,自己实例化对象
	BpBinder *bp = new BpBinder(id, buf);
	bp->test_BpBinder();
	bp->test_sp();
	bc.m_ptr->test_sp();
	return 0;
}
4.程序例子02
#includeusing namespace std;

templateclass sp {
public:
	templatestatic inline spmake(Args&&... args);

	void test_sp(){
		printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
	}
	
	T* m_ptr;
};

templatetemplatespsp::make(Args&&... args) {
	T* t = new T(std::forward(args)...);
	spresult;
    //t为传进来类的实例,被保存在成员变量m_ptr中;而result被定义为模板类返回的。
	result.m_ptr = t;
	return result;
}

//class BpBinder : public sp{
class BpBinder {
public:
	BpBinder(int id, char *name){
		printf("id = %d, name = %s\n",id,name);
	}

	void test_BpBinder(){
		printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
	}
	int num;
};

int main(){
	int id = 44;
	char buf[] = "ServiceManager";
    //0.将BpBinder类类型传进sp,将BpBinder的参数传给make()函数来实例化对象
	spbc = sp::make(id, buf);
    //spbc = sp::make(1234, const_cast("ServiceManager"));
    //1.调用智能指针模板类sp的test_sp()函数
	bc.test_sp();
    //2.调用BpBinder类的test_BpBinder()函数
	bc.m_ptr->test_BpBinder();

	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


标题名称:Android系统之仿sp智能指针实例-创新互联
分享URL:http://bjjierui.cn/article/dieeog.html

其他资讯