条款21:必须返回对象时,别妄想返回其reference

const Rational & operator* (const Rational & lhs, const Rational & rhs);

{

        Rational result (lhs.n * rhs.n , lhs.d * rhs.d);

        return result;

}

如果定义一个本地变量,就是在stack空间创建对象。

问题是:这个函数返回一个reference指向result,但result是个local对象,而local对象在函数退出前被销毁了,因此这个oprator * 并未返回reference指向某个Rational;

于是,让我们考虑在堆内构造一个对象,并返回reference指向它。对象由new创建

const Rational operator * (const Rational & lhs, const Rational & rhs)

{

     Rational * result = new Rational(lhs.n * rhs.n ,lhs .d * rhs .d);

     return * result ;

}

但是问题是谁该对你的new对象进行delete?

而且 就算有有意识去防止内存泄漏,但是还是很难避免。

Rational w, x, y, z;

w= x*y *z;

这里调用两次operatior * ,因为无法跟踪后两次new的对象,所以会造成内存泄漏。

 

如果你一心想去避免构造函数和析构函数,可能会想到用static Rational对象:

const Rational & operator * (const Rational & lhs, const Rational & rhs);

{

         static Rational result;

          result = .....;

         return result ;

}

bool operator ==(const Rational & lhs , const Raional & rhs);

Rational  a, b, c, d;

...

if((a*b) == (c*d))

{

        相等时,做什么动作

}else

{           

           不相等时,做什么动作

}
问题是:两个operator都会起作用,但是最后一次得operator的调用会覆盖前面的,所以一直相等,所以一直为true;

所以最佳的做法:

inline const Rational operator * (const Raional & lhs , cosnt Ratioanl & rhs )

{

        return Rational ( lhs.n * rhs .n, lhs.d * rhs .d);

}

当你必须返回一个reference和返回一个对象时之间抉择时,你的工作即使挑出行为正确的那个,而不是考虑成本的问题!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,我可以帮你编写一个简单的Python脚本来实现山海经跑图。 首先你需要导入两个Python库:PIL和pytesseract。PIL库用于图像处理,pytesseract库用于OCR识别文字。 接下来,你需要从图片中提取出文字。你可以使用PIL库中的Image模块打开图片,然后使用pytesseract库中的image_to_string函数将图片中的文字转换为字符串。 然后,你需要将山海经中的地名和路线提取出来。你可以使用Python的正则表达式来匹配这些字符串,并将它们保存到一个列表中。 最后,你可以使用Python的Web自动化库(如selenium)来模拟人工操作,让程序自动跑图。 以下是一个简单的Python脚本,用于提取山海经中的地名和路线,并自动跑图: ```python from PIL import Image import pytesseract import re from selenium import webdriver import time # 打开图片 img = Image.open('shanhaijing.jpg') # OCR识别图片中的文字 text = pytesseract.image_to_string(img, lang='chi_sim') # 提取地名和路线 place_pattern = re.compile(r'【(.*?)】') # 地名正则表达式 route_pattern = re.compile(r'→(.*?)\n') # 路线正则表达式 places = place_pattern.findall(text) routes = route_pattern.findall(text) # 打印地名和路线 for i in range(len(places)): print(f'{i + 1}、{places[i]}:{routes[i]}') # 自动跑图 driver = webdriver.Chrome() driver.get('https://shanhaimap.mihoyo.com/') # 点击开始游戏按钮 start_game_btn = driver.find_element_by_class_name('start-game') start_game_btn.click() # 等待动画播放完成 time.sleep(5) # 点击跑图按钮 run_btn = driver.find_element_by_class_name('run-btn') run_btn.click() # 依次点击地名和路线 for i in range(len(places)): time.sleep(1) place_btn = driver.find_element_by_xpath(f"//*[text()='{places[i]}']") place_btn.click() time.sleep(1) route_btn = driver.find_element_by_xpath(f"//*[text()='{routes[i]}']") route_btn.click() ``` 请注意,这只是一个简单的脚本,可能需要根据你的具体情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值