Important: Please read the Qt Code of Conduct -

((TexoDemo*)prm) meaning what?

  • Hi,

    I am trying to understand one project written by others. Here are a few lines which I have to difficult to understand.

    int TexoDemo::onData(void* prm, unsigned char* data, int)
    // go past the frame tag
    ((TexoDemo*)prm)->wDisplay->display(((char*)data) + 4);
    ((TexoDemo*)prm)->iDisplay->setImgData(((char*)data) + 4);
    return 1;

    Where TexoDemo is a class and onData is a function defined inside this class. wDisplay is a UI widget (promoted one).

    The bit that I do not understand is the grammar ((TexoDemo*)prm). What is this supposed to mean? I know this might be a C++ basic but I just don't know where to look it up. I don't even know how to search for this C++ problem.

    I would appreciate the help from anyone. Thank you.


  • @zxw2006063

    That is pointer casting. Like in here

    Or also in here
    it is better to use for instance dynamic_cast or one of the others

  • Qt Champions 2017

    prm is the void argument for your onData function. wDsiplay must be the member variable inside the class TexoDemo. To access this variable you need to typecast the prm to TexoDemo type. If you don't typecast, wDisplay will not be able to access.
    display must be a function of wDisplay.

    Hope that clarifies.

  • @koahnig Thanks. Then I can look it up from the pointer-casting.

  • @dheerendra

    Thank you Dheerendra for your detailed explanation.
    I just have one more question. You said the wDisplay will not able to access if I don't do typecast. But I found another function defined in the same class as below:

    void TexoDemo::onAmp(int value)

    Why in this case the wDisplay can be accessed and no need to use the typecast? Sorry if the question is too simple. Thank you in advance.


  • Qt Champions 2017

    in the current case it is the current objects wDisplay variable u r accessing. In the previous case it is wDisplay of another object passed through prm variable.

  • @dheerendra
    Clear. Thank you.

  • Qt Champions 2017

    move the issue to "Solved" state

Log in to reply