C_and_CPP 板


LINE

※ 引述《gn00618777 (非常念旧)》之铭言: : 最近在 trace android 的 code ,里面有些 c++ 的程式,写了一小段来验证 : #include<iostream> : #include<stdlib.h> : using namespace std; : template<typename T> : class Demo{ : public: : Demo(T* other); : Demo(const Demo<T>& other); : }; : template<typename T> : Demo<T>::Demo(T* other){ : cout<<"This is the First constructor"<<endl; : } : template<typename T> : Demo<T>::Demo(const Demo<T>& other){ : cout<<"This is the second constructor"<<endl; : } : int main(){ : int *ptr; : Demo<int> p; // 会 error,因为它会找不到 Demo() 建构子,这个合理~ : Demo<int> p = ptr; //这边他会 call Demo(T* other) 这边的建构子 : //不太懂为啥他会call 第一个建构子呢? : //如果用 Demo<int> p(ptr); 还比较容易理解.. : } : 谢谢 板友提供的关键字 copy initialization,让我大概了解 int *ptr; Demo<int> p = ptr; --> 会去判断等号右边的型态,进而去寻找 Demo 中符合 的建构子,ptr 是一个指标,所以去 call Demo(T*other) 在Android源码常会看到下面写法 sp<Camera> c = new Camera(); 不太懂 c 的意义是甚麽,代表可以用c去存取 Camera类别内的方法吗?我去试下面 范例 code Camera 用 myClass 取代, sp 用 Demo 取代 class Demo{ public: Demo(T* other); Demo(const Demo<T>& other); }; template<typename T> Demo<T>::Demo(T* other){ cout<<"This is the First constructor"<<endl; } template<typename T> Demo<T>::Demo(const Demo<T>& other){ cout<<"This is the second constructor"<<endl; } class myClass{ public: myClass():a(0){} int getA(){return a} private: int a; }; int main(){ Demo<myClass> c = new myClass(); // call Demo(T * other) 建构子没问题 cout<<c->getA()<<endl; // 这边就错了,也许我搞不清楚 c 到底是甚麽 // [Error] base operand of '->' has non-pointer type 'sp<myClass>' } 能否板友再指点我一下呢...谢谢! --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 58.115.110.72
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1461507127.A.85C.html ※ 编辑: gn00618777 (58.115.110.72), 04/24/2016 22:14:35
1F:→ Caesar08: Demo class templace没有getA的data member function 04/24 22:24
2F:→ bibo9901: 跟template无关 纯粹是你没弄清楚物件的用法 04/24 22:25
3F:→ bibo9901: -> 是给指标用的. c不是指标当然不能用 04/24 22:26
4F:→ gn00618777: new 这个用法是传回记忆体位址,所以我很自然的把 c 04/24 22:32
5F:→ gn00618777: 当成一个指标去接这位址... 04/24 22:33
6F:→ gn00618777: 以Demo<myClass> 宣告的 c 看起来是一个物件 04/24 22:34
7F:→ gn00618777: c 去接这个位址整个就不通 04/24 22:35
8F:推 BlazarArc: 我不知道android source code, 不过那个sp看起来就很像 04/24 23:22
9F:→ BlazarArc: smart pointer? 04/24 23:22
10F:→ BlazarArc: smart pointer会去overload -> operator 04/24 23:24
11F:→ gn00618777: 是的,他是个smart point,也有看到它overload -> 04/24 23:36
12F:→ gn00618777: overload operator -> 的用法应该是 c-> 成员 04/24 23:39
13F:→ gn00618777: 我不解的是 new myClass 这个记忆体位址,它在 sp 内 04/24 23:39
14F:→ gn00618777: 它是如何被指定到 sp 类别的哪些成员变数或是某些操作 04/24 23:40
15F:推 BlazarArc: http://preview.tinyurl.com/qfvm482 04/24 23:57
16F:→ BlazarArc: Built-in member access operators 04/24 23:58
17F:→ BlazarArc: If a user-defined operator-> is provided ... 04/24 23:58







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP