C_and_CPP 板


LINE

......先行声明,以下内容大多出自於猜测 如有错误希望大家可以出声指正,以免造成误导与混淆 :( : ======================== : 你的问题其实只有一个关键: : ic::ic(src& s) : 接受的是 non-const reference : 你传暂时物件进去,那就等於传 const 物件了,这样当然会错罗 : 否则你可以试试把 ic(src& s) 参数改成 const src &s 看看 : 就可以运作了 : 函式的 reference 参数 : 如果没加 const,就表示接收进来的物件可能被修改 : 所以如果有传入暂时物件的可能,一定要加 const : ========== 我觉得不太像是这个问题,我写几个测试 code 根据原 po 的问题改编,g++ 3.4.2 得错误讯息 error: no matching function for call to `DerivedA::DerivedA(Derived A(&)(DerivedB))' note: candidates are: DerivedA::DerivedA(const DerivedA&) note: DerivedA::DerivedA(const Base&) struct Base{Base(){}}; struct DerivedA: public Base{ DerivedA(const Base&){} DerivedA(const DerivedA&){} }; struct DerivedB: public Base{ DerivedB(){} DerivedB(const Base&){} }; int main(){ DerivedA a(Base()); DerivedA b(DerivedB(a)); DerivedA c(b); // at this line } b 是一个普通的 DerivedA 物件,可是 const DerivedA& 和 const Base& 都不吃 根据错误讯息,b 的型别是 Derived A(&)(DerivedB) 再来看 Comeau C++ 4.3.3 的错误讯息: (from: http://www.comeaucomputing.com/tryitout/) (btw, 我好想买喔) error: no instance of constructor "DerivedA::DerivedA" matches the argument list The argument types that you used are: (DerivedA (DerivedB)) DerivedA c(b); ^ 也就是说,他认为 b 的型别是 DerivedA (DerivedB) 了 把上述 code 做一点修正: 把 struct DerivedA 移到 DerivedB 下面 在 DerivedA 里追加一个 c'tor 叫 DerivedA(const DerivedA(DerivedB)){} (模仿 Comeau 的型别) 在 g++ 下,再得错误讯息:(我只列改变) note: candidates are: DerivedA::DerivedA(const DerivedA(*)(DerivedB)) <near match> ... 看到 g++ 的提示,near match...(差一个 const) 而 Comeau 的 compile 结果呢? 很抱歉,还是 error, 不过给的错误讯息没有任何改变 (这点就觉得 g++ 错误讯息比较方便了… 有时候看不懂的型别就 compile 没差,反正会告诉你) 再改一次,这次加个 & DerivedA(const DerivedA& (DerivedB)){} 错误讯息: note: candidates are: DerivedA::DerivedA(const DerivedA&(*)(DerivedB)) <near match> ... 这次改成 g++ 的提示: DerivedA(const DerivedA(&)(DerivedB)){} 这样…还是不能通过编译 note: candidates are: DerivedA::DerivedA(const DerivedA(&)(DerivedB)) ... 这次连 <near match> 都没给了 把 const 拿掉,变成: DerivedA(DerivedA(&)(DerivedB)){} 这下终於顺利通过 compile 了,g++ 和 comeau 都一样 而如果改成这样: DerivedA(DerivedA(DerivedB)){} 其实也可以通过编译,同样 g++ 和 comeau 都一样 这样也可以: DerivedA(DerivedA(*)(DerivedB)){} ok, 回头去看其中一个型别: typeA(*)(typeB) 不觉得很像 function pointer 吗? 那麽 typeA(&)(typeB) 又是什麽? 答案是实体 function...(我居然测试测半天才发现这个事实) 请再回到最早的 code, 并在最下面加上这个: Base base; b(base); b(Base()); 得 g++ 错误讯息 undefined reference to `b(DerivedB)' undefined reference to `b(DerivedB)' comeau c++... online 版没有 link, 所以没有错误 :Q ok, 说得乱七八糟,讲个结论吧 ...嗯,刚刚本来想举些例子做结论 不过举了几个例子之後又发现一些奇妙的东西 比方说 int value = 5; TypeA a(TypeB(value)); 或 const int value = 5; TypeA a(TypeB(value)); 则 a 是一个 function TypeA a(TypeB(5)); 则 a 是一个 TypeA 物件,用 TypeB::TypeB(int) 建立的暂时物件建立的物件 我被这搞糊涂了... 不过看来假使我这样写: int(int) 则这代表型别: int(*)(int) 或 int(&)(int) 我想这也是为什麽 boost::function<int(int)> 可以这样用了 另外 g++ 有时候也会将 int(int) 视为 int(&)(int) 我想这可能是最佳化机制的缘故…(纯猜测) (昏倒, C++ 好谜... 避免使用这种吊诡的用法吧) (毕竟平时应该不会用暂时物件来建立物件吧) (再钻研下去似乎过於没意义了) -- By Gamers, For Gamers - from the past Interplay --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.132.128.238 ※ 编辑: godfat 来自: 220.132.128.238 (02/09 04:27)







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灯, 水草

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

TOP