What could cause a dynamic_cast to crash?

前端 未结 6 1069
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 16:58

I have a piece of code looking like this :

TAxis *axis = 0;
if (dynamic_cast(obj))
   axis = (dynamic_cast         


        
6条回答
  •  执笔经年
    2020-12-06 17:35

    I suggest using a different syntax for this code snippet.

    if (MonitorObjectH1C* monitorObject = dynamic_cast(obj))
    {
        axis = monitorObject->GetXaxis();
    }
    

    You can still crash if some other thread is deleting what monitorObject points to or if obj is crazy garbage, but at least your problem isn't casting related anymore and you're not doing the dynamic_cast twice.

提交回复
热议问题