C_and_CPP 板


LINE

// 程式码备份: http://nopaste.info/6b4a9226e5.html // 程式码备份: http://src.wtgstudio.com/?vw8A8a #include <iostream> using namespace std; template <typename T> class LineInterface { private: public: typedef LineInterface<T> this_type; explicit inline LineInterface(void) {} inline LineInterface(const this_type& d) {} virtual inline ~LineInterface(void) {} inline this_type& operator=(const this_type& d) { if (this != &d) {} return *this; } inline virtual const T& GetX1() const = 0; inline virtual const T& GetX2() const = 0; }; template <typename T> class StartEndLine : public LineInterface<T> { private: T m_x1, m_x2; public: typedef StartEndLine<T> this_type; explicit inline StartEndLine(const T& x1 = T(), const T& x2 = T()): m_x1(x1), m_x2(x2) {} inline StartEndLine(const this_type& d): m_x1(d.m_x1), m_x2(d.m_x2) {} virtual inline ~StartEndLine(void) {} inline this_type& operator=(const this_type& d) { if (this != &d) { m_x1 = d.m_x1; m_x2 = d.m_x2; } return *this; } inline const T& GetX1() const { return m_x1; } inline const T& GetX2() const { return m_x2; } }; template <typename T> class StartSameWidthLine : public LineInterface<T> { private: static T m_width; T m_x1; public: typedef StartSameWidthLine<T> this_type; explicit inline StartSameWidthLine(const T& x1 = T()): m_x1(x1) {} inline StartSameWidthLine(const this_type& d): m_x1(d.m_x1) {} virtual inline ~StartSameWidthLine(void) {} inline this_type& operator=(const this_type& d) { if (this != &d) { m_x1 = d.m_x1; } return *this; } inline static void SetWidth(const T& w = T()) { m_width = w; } inline const T& GetX1() const { return m_x1; } inline const T& GetX2() const { return m_x1 + m_width; } }; template<typename T> T StartSameWidthLine<T>::m_width = T(); template <typename T> inline ostream& operator<<(ostream& os, const LineInterface<T>& d) { return os << "(" << d.GetX1() << "," << d.GetX2() << ")"; } int main(int argc, char* argv[]) { cout << StartEndLine<int>(10, 20) << endl; StartSameWidthLine<int>::SetWidth(5); cout << StartSameWidthLine<int>(10) << endl; system("PAUSE"); return 0; } // 程式码备份: http://nopaste.info/6b4a9226e5.html // 程式码备份: http://src.wtgstudio.com/?vw8A8a 错误讯息: .\CPP_AND_C.cpp(xxx) : warning C4172: 传回区域变数或暂存的位址 .\CPP_AND_C.cpp(xxx) : 编译类别 样板 成员函式 'const int &StartSameWidthLine<T>::GetX2(void) const' 如果我将 const int &StartSameWidthLine<T>::GetX2(void) const 改成 const int StartSameWidthLine<T>::GetX2(void) const 那麽就违反 没有完成所有从 LineInterface<T> 继承而来的Pure virtual function 如果我将 const int &LineInterface<T>::GetX2(void) const 改成 const int LineInterface<T>::GetX2(void) const 那麽就对 StartEndLine<T> 来说这是不必要的复制 请问我该怎麽做呢? --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.195.88.23
1F:推 legnaleurc:就把 const reference 拿掉呗,让编译器去烦恼最佳化 04/30 14:03
2F:→ legnaleurc:Super class 也不应该假设回传值一定有个 reference 吧 04/30 14:04
3F:→ chrisdar:恩 这样说好像也很有道理耶 XD 04/30 14:23
4F:→ chrisdar:根据各方的意见 决定采用(#19-JrOLY)方法二 04/30 18:20







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

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

TOP