目錄
1.仿sp實(shí)例?
2.其實(shí)本質(zhì)是普通類繼承模板類的一個(gè)例子。參考之前寫(xiě)過(guò)的一篇,幫助理解。
3.程序例子01
4.程序例子02
為了便于理解Android智能指針模板類make()創(chuàng)建的過(guò)程,寫(xiě)了一個(gè)仿智能指針的一個(gè)例子,方便初學(xué)者理解。
2.其實(shí)本質(zhì)是普通類繼承模板類的一個(gè)例子。參考之前寫(xiě)過(guò)的一篇,幫助理解。C++普通類繼承模版類demo_Android系統(tǒng)攻城獅的博客-博客
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.普通類繼承模板類,在模板類中實(shí)例化普通類,并返回普通類的實(shí)例對(duì)象
spbc = sp::make(id, buf);
//bc.test_BpBinder(); //failed
bc.test_sp();
bc.m_ptr->test_sp();
//2.第二種寫(xiě)法
spbb;
bb.make(id, buf);
//3.不借用模板類sp,自己實(shí)例化對(duì)象
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為傳進(jìn)來(lái)類的實(shí)例,被保存在成員變量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類類型傳進(jìn)sp,將BpBinder的參數(shù)傳給make()函數(shù)來(lái)實(shí)例化對(duì)象
spbc = sp::make(id, buf);
//spbc = sp::make(1234, const_cast("ServiceManager"));
//1.調(diào)用智能指針模板類sp的test_sp()函數(shù)
bc.test_sp();
//2.調(diào)用BpBinder類的test_BpBinder()函數(shù)
bc.m_ptr->test_BpBinder();
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前文章:Android系統(tǒng)之仿sp智能指針實(shí)例-創(chuàng)新互聯(lián)
本文URL:http://jinyejixie.com/article6/dieeog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、品牌網(wǎng)站建設(shè)、App開(kāi)發(fā)、服務(wù)器托管、營(yíng)銷型網(wǎng)站建設(shè)、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容