用 (*it).m 还是 it->m

用 (*it).m 还是 it->m

摘自《Extended STL》

标准库要求,所有值类型为聚合类型的迭代器必须支持指针成员选取运算符(operator ->()),下面是使用该运算符的示例代码:

struct X
{
  int x;
};

some_iterator<X>  si  = . . .
some_iterator<X>  si2 = . . .
some_iterator<X>  end = . . .

if( end != si &&
    end != si2)
{
  si->x = si2->x;
}

标准(C++-03: 24.1.1;1)要求,对一个迭代器应用指针成员选取运算符,在语义上等同于先对其应用解引用运算符,再应用点号成员选取运算符,即it->m与(*it).m等效。

可惜,使用该运算符会遇上麻烦。

假设我们有一个容器类型C,它的实例保存智能指针类型P的实例,P用于管理对象生存期。P上定义了一个release()方法用于提早释放对象。进一步假设,被P管理的类型T上也定义了一个release()方法。在下面代码片断中,我们希望通过该容器的迭代器类型I的一个实例,调用T::release()方法:

C   cont = . . .
I   it   = cont.begin();
it->release();

不幸的是,这段代码调用的不是T::release()方法,它调用的是P::release()方法,从而销毁了T的实例。当我们再次使用cont这个容器的时候,就可能遇到各种奇怪的问题,以下代码才是真正实现我们想法的代码:

C   cont = . . .
I   it   = cont.begin();
it->->release();                                                          

可是C++并不支持这样的语句,且理由充分。(想象一下,如果C++支持这样的语句,"模糊C++代码"大赛上,会有多少参赛代码,争着在一条语句中塞进最多的operator->()调用!)

为使C++满足我们的想法,我们必须放弃成员选取运算符而转用解引用运算符,就像以下代码示例中一样:

C   cont = . . .
I   it   = cont.begin();
(*it)->release();                                                         

这是迭代器语法中恼人的小缺点。

在迭代器中支持指针成员选取运算符,没什么实质的意义,而仅仅是语法糖。我认为标准中将这规定为迭代器概念的一个特性是严重的错误。在我的日常工作中,除了仅有几个特例外,我完全避免使用迭代器的指针成员选取运算符,而使用解引用和点号成员选取运算符。我建议你也这么做。

Tip: Prefer iterator dereference and the dot member selection operator ((*it).m) over the pointer member selection operator (it->m).

提示: 使用解引用及点号成员选择运算符((*it).m)代替指针成员选择运算符(it->m)。

(转载请注明来源于金庆的专栏)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
% SolarCollector.m % ---------------------------------------------------------------------------------------------------------------------- % % Simple first-order solar collector model (M-file called by TRNSYS type 155) % % Data passed from / to TRNSYS % ---------------------------- % % trnTime (1x1) : simulation time % trnInfo (15x1) : TRNSYS info array % trnInputs (nIx1) : TRNSYS inputs % trnStartTime (1x1) : TRNSYS Simulation Start time % trnStopTime (1x1) : TRNSYS Simulation Stop time % trnTimeStep (1x1) : TRNSYS Simulation time step % mFileErrorCode (1x1) : Error code for this m-file. It is set to 1 by TRNSYS and the m-file should set it to 0 at the % end to indicate that the call was successful. Any non-zero value will stop the simulation % trnOutputs (nOx1) : TRNSYS outputs % % % Notes: % ------ % % You can use the values of trnInfo(7), trnInfo(8) and trnInfo(13) to identify the call (e.g. first iteration, etc.) % Real-time controllers (callingMode = 10) will only be called once per time step with trnInfo(13) = 1 (after convergence) % % The number of inputs is given by trnInfo(3) % The number of expected outputs is given by trnInfo(6) % WARNING: if multiple units of Type 155 are used, the variables passed from/to TRNSYS will be sized according to % the maximum required by all units. You should cope with that by only using the part of the arrays that is % really used by the current m-File. Example: use "nI = trnInfo(3); myInputs = trnInputs(1:nI);" % rather than "MyInputs = trnInputs;" % Please also note that all m-files share the same workspace in Matlab (they are "scripts", not "functions") so % variables like trnInfo, trnTime, etc. will be overwritten at each call. % % ---------------------------------------------------------------------------------------------------------------------- % This example implements a very simple solar collector model. The component is iterative (should be called at each % TRNSYS call) % % trnInputs % --------- % % trnInputs(1) : Ti, collector inlet temperature % trnInputs(2) : mdot, collector flowrate % trnInputs(3) : Tamb , ambient temperature % trnInputs(4) : Gt, solar radiation in the collector plane % % trnOutputs解释下这段代码
最新发布
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值