jeudi 8 septembre 2022

QGraphicsItem* conversion into long using reinterpret_cast

I am creating test cases for my code. For that, I am capturing my code ( functions ) in Capture Mode and in Run mode I am runnig that captured code. And if both are giving me same output, then my test case is pass otherwise it is fail.

Capture code (which is already written ) does not accept non primitive data types.
Means if some function has Non primitive data types as arguments, then it will not accept. So I need to write a capture function in such a way that it's argument will be primitive data types.

So the functions which has non primitive data types as arguments, I am converting them into primitive data type and then after capturing, again converting into it's original type using reinterpret_cast.
Let us say I have following function :

SelectObject()
{
  QGraphicsItem* currentSelectedItem = _scene->itemAt( some location );      
  long currentItem = reinterpret_cast<long>(currentSelectedItem);
  SelectObjectCapture(currentItem);
}

SelectObjectCapture(long currentSelectedItem)
{
    CAPTURE FUNCTION(SelectObjectCapture,currentSelectedItem);
    QGraphicsItem* currentItem = reinterpret_cast<QGraphicsItem*>(currentSelectedItem);   
    
    // then using currentItem in code
}     

But this currentItem is leading to crash in RUN MODE. It perfectly works fine in Capture mode or without any mode.
Am I correct in reinterpret_cast ?

Aucun commentaire:

Enregistrer un commentaire