Passing a Class as a Pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crispin
    New Member
    • Jan 2007
    • 11

    Passing a Class as a Pointer

    Hi Everyone,

    I was hoping one of you kind souls could help me with the following:

    I have a class method which initializes audio libraries written in C, shown below... where it says (RIGHT HERE) I am trying to create a pointer to the class MiniHost, am I doing it right?

    >>>>>

    void MiniHost::setup Audio()
    {
    ////Some unimportant stuff here, then:

    Pa_OpenStream(
    &stream,
    NULL,
    &outputParamete rs,// ignore all this stuff
    kSampleRate,
    kBlockSize,
    NULL,
    patestCallback,
    this); /////////// <- RIGHT HERE

    Pa_StartStream( stream );

    }

    >>>>>

    Here is the second part, a static method from which I am trying to call the class listed above:

    >>>>>>>>>
    static int patestCallback( const void *inputBuffer, void *outputBuffer,
    unsigned long framesPerBuffer ,
    const PaStreamCallbac kTimeInfo* timeInfo,
    PaStreamCallbac kFlags statusFlags,
    void *userData ) //I can't change this stuff, the libraries need it this way
    {

    userData->someFunction() ; //dosen't work

    return paContinue;
    }

    >>>>>>>>>

    What I need to do in the above code, is to call MiniHost through the 'userData' parameter, into which I tried to pass a pointer to the class.
    Assuming I passed the pointer correctly, how do I call the class?

    Thanks a lot!
    Crispin
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by crispin
    Code:
    static int patestCallback( const void *inputBuffer, void *outputBuffer,
                                unsigned long framesPerBuffer,
                                const PaStreamCallbackTimeInfo* timeInfo,
                                PaStreamCallbackFlags statusFlags,
                                void *userData ) //I can't change this stuff, the libraries need it this way
    {
    
       userData->someFunction(); //dosen't work
    
    return paContinue;
    }
    What I need to do in the above code, is to call MiniHost through the 'userData' parameter, into which I tried to pass a pointer to the class.
    Assuming I passed the pointer correctly, how do I call the class?

    Thanks a lot!
    Crispin
    I'm not sure the header syntax is correct - userData is defined as a void*, or a pointer to nothing. Maybe you can cast userData to a MiniHost*, and then try ->someFunction() , as in:

    Code:
    static int patestCallback( const void *inputBuffer, void *outputBuffer,
                                unsigned long framesPerBuffer,
                                const PaStreamCallbackTimeInfo* timeInfo,
                                PaStreamCallbackFlags statusFlags,
                                void *userData ) //I can't change this stuff, the libraries need it this way
    {
       userData = (MiniHost*)userData;
       userData->someFunction(); //dosen't work
    
    return paContinue;
    }

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Hi. A void pointer needs to be recast
      Code:
      ((MiniHost*)userData)->someFunction(); //should work

      Comment

      • crispin
        New Member
        • Jan 2007
        • 11

        #4
        Hi,

        Thanks for your help... I got it working!

        I passed the class as 'this'. And added 'MiniHost *m = (MiniHost*)user Data;' in the static int. To use the methods I simply type m->whatever();

        Cheers,
        Crispin

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by crispin
          Hi,

          Thanks for your help... I got it working!

          I passed the class as 'this'. And added 'MiniHost *m = (MiniHost*)user Data;' in the static int. To use the methods I simply type m->whatever();

          Cheers,
          Crispin
          Be careful. At this point you have 2 pointers pointing to the same class. An unnecessary risk

          Comment

          Working...