call a function without the required arguments? is it possible?

Aug 16, 2012 at 12:27pm
Hi,

I came across an example in a project that I am working on like this

this is the definition of the function
1
2
3
4
5
//---------------------------------------------------------------------------
int TFmDBViewer::SweepBatteryTestRawDataEx(char *pData, char *key, int nFlags)
{
	return(SweepBatteryTestRawData(pData,key));
}


This is the declaration
 
int SweepBatteryTestRawDataEx(char *pData, char *key, int nFlags );


this is how it is called
 
BatteryRecord.SweepProbeHistory(Store,SweepBatteryTestRawDataEx);


what bugged me was that they called the function without the arguments required. and the IDE compiled.

any explanation would be appreciated.

much thank

ThangDo
Aug 16, 2012 at 12:40pm

BatteryRecord.SweepProbeHistory(Store,SweepBatteryTestRawDataEx);
This does not look like a call to the function SweepBatteryTestRawDataEx, but rather using it as an input parameter.

What are the input parameter types for the function SweepProbeHistory?
Aug 16, 2012 at 1:19pm
BatteryRecord.SweepProbeHistory(Store,SweepBatteryTestRawDataEx);

This construction is not a call to SweepBatteryTestRawDataEx. It is a call to member function SweepProbeHistory with two arguments: Store and SweepBatteryTestRawDataEx.
It looks like that inside the body of SweepProbeHistory function SweepBatteryTestRawDataEx is called. You should look through the code of SweepProbeHistory that to see how SweepBatteryTestRawDataEx is called.



Aug 16, 2012 at 1:59pm
Yeah, I think the second parameter there is passing in the start address for SweepBatteryTestRawDataEx.

Not sure what the second parameter requirement is for SweepProbeHistory. Void pointer or function pointer, maybe?
Last edited on Aug 16, 2012 at 2:00pm
Aug 16, 2012 at 2:06pm

i@Hutch105
Not sure what the second parameter requirement is for SweepProbeHistory. Void pointer or function pointer, maybe?


A function pointer may not be implicitly converted to a void pointer.
Aug 16, 2012 at 2:29pm
vlad from moscow wrote:
A function pointer may not be implicitly converted to a void pointer.


I never said it did.

My point is that this will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void MyFunc()
{
   // Some stuff
}

void MyPassVoid(void *ptr)
{
   cout << "ptr stores " << ptr << endl;
}

void MyPassFuncPtr(void (*ptr)(void))
{
   cout << "ptr stores " << ptr << endl;
}

int main()
{
   MyPassVoid(MyFunc);
   MyPassFuncPtr(MyFunc);
}


Granted, passing the void pointer would probably be a bit useless as it wouldn't be able to call the function inside there.
Aug 16, 2012 at 2:41pm
The reason why I think it is a call to a function is because I traced it on the call stack of the IDE, and I ended up with .
1
2
3
4
5
//---------------------------------------------------------------------------
int TFmDBViewer::SweepBatteryTestRawDataEx(char *pData, char *key, int nFlags)
{
	return(SweepBatteryTestRawData(pData,key));
}


the declaration of SweepProbeHistory is as follow
 
int SweepProbeHistory(TObjectStore *Store, int(__closure *Process )(char *, char *, int ), bool fOldData=true);


does it have anything to do with the int(__closure *Process )(char *, char *, int ) ?
Last edited on Aug 16, 2012 at 2:43pm
Aug 16, 2012 at 2:50pm
int(__closure *Process )(char *, char *, int ) is a declaration of a function pointer to which function SweepBatteryTestRawDataEx(char *pData, char *key, int nFlags) is converted when is passed as an argument to function SweepProbeHistory.
Topic archived. No new replies allowed.