473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing a Class as a Pointer

11
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::setupAudio()
{
////Some unimportant stuff here, then:

Pa_OpenStream(
&stream,
NULL,
&outputParameters,// 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 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
Jan 31 '07 #1
4 1957
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. static int patestCallback( const void *inputBuffer, void *outputBuffer,
  2.                             unsigned long framesPerBuffer,
  3.                             const PaStreamCallbackTimeInfo* timeInfo,
  4.                             PaStreamCallbackFlags statusFlags,
  5.                             void *userData ) //I can't change this stuff, the libraries need it this way
  6. {
  7.  
  8.    userData->someFunction(); //dosen't work
  9.  
  10. return paContinue;
  11. }
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:

Expand|Select|Wrap|Line Numbers
  1. static int patestCallback( const void *inputBuffer, void *outputBuffer,
  2.                             unsigned long framesPerBuffer,
  3.                             const PaStreamCallbackTimeInfo* timeInfo,
  4.                             PaStreamCallbackFlags statusFlags,
  5.                             void *userData ) //I can't change this stuff, the libraries need it this way
  6. {
  7.    userData = (MiniHost*)userData;
  8.    userData->someFunction(); //dosen't work
  9.  
  10. return paContinue;
  11. }
Jan 31 '07 #2
willakawill
1,646 1GB
Hi. A void pointer needs to be recast
Expand|Select|Wrap|Line Numbers
  1. ((MiniHost*)userData)->someFunction(); //should work
Jan 31 '07 #3
crispin
11
Hi,

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

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

Cheers,
Crispin
Jan 31 '07 #4
willakawill
1,646 1GB
Hi,

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

I passed the class as 'this'. And added 'MiniHost *m = (MiniHost*)userData;' 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
Jan 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.