Search code examples
c++qtreinterpret-cast

reinterpret_cast changes type from `const float *` to `float` unexpectedly


I have a pointer:

const float  *m_posBufferPtr_float;

I assign to this variable with:

m_posBufferPtr_float = reinterpret_cast<const float *>(buffer()->data().constData());

At which constData() function returns const char * type:

inline const char *QByteArray::constData() const
{ return d->data(); }

Therefore my reinterpret_cast should convert const char * to const float *.


But to my surprise, exactly before reinterpret_cast my pointer is:

Before

and exactly after reinterpret_cast debugger shows my pointer as:

After


I wonder why reinterpret_cast is converting const char * to float rather than const float *


Solution

  • If you take this snippet:

    int main(int argc, char *argv[])
    {
        float* pointer = nullptr;
    
        float value = 12.34;
        pointer = &value;
    
    
        qDebug() << *pointer;
    }
    

    and execute it step by step, you will see in your debugger:

    enter image description here

    Then

    enter image description here

    Notice that the type became float when the pointer has been initialized. It's due to the configuration of the debugger.

    In Qt Creator, right-click on your pointer and uncheck Dereference pointers automatically:

    enter image description here