C++ experts, please come in and help (dec 14 09)
by Chin Liu · in Torque 3D Professional · 12/14/2009 (4:01 pm) · 8 replies
Hi,
Yesterday, when I explored the source code of the engine, I ran into the code below:
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
this is the link to the TGEA document http://docs.torquepowered.com/tgea/official/content/documentation/Engine%20Reference/a04938.html
Under Typedefs, it is the first line of code.
Here is my thinking:
1) First, I know typedef is used to give a data type a new name, for example:
typedef int Mytype
so, in the code, I can write
Mytype num = 9;
Instead of
int num = 9;
However, this cannot be applied to
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
2) Second, I think this may be a function pointer, for example
int (*funcpt) (int, int)
where funcpt is a pointer to a type of function that returns an int value and takes in two int values
If the line of code were
typedef const char * (*StringCallback) (SimObject *obj, S32 argc, const char *argv[])
Then, I can say StringCallback is a function pointer with:
Inputs => SimObject * obj, S32 argc, const char *argv[])
Output => char*
But this is not the case unfortunately
Just now, I tried to ask everyone in my company (they do C++ for living), but not a single one came up with a reasonable answer.
Can someone explain this to me?
Thanks
Yesterday, when I explored the source code of the engine, I ran into the code below:
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
this is the link to the TGEA document http://docs.torquepowered.com/tgea/official/content/documentation/Engine%20Reference/a04938.html
Under Typedefs, it is the first line of code.
Here is my thinking:
1) First, I know typedef is used to give a data type a new name, for example:
typedef int Mytype
so, in the code, I can write
Mytype num = 9;
Instead of
int num = 9;
However, this cannot be applied to
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
2) Second, I think this may be a function pointer, for example
int (*funcpt) (int, int)
where funcpt is a pointer to a type of function that returns an int value and takes in two int values
If the line of code were
typedef const char * (*StringCallback) (SimObject *obj, S32 argc, const char *argv[])
Then, I can say StringCallback is a function pointer with:
Inputs => SimObject * obj, S32 argc, const char *argv[])
Output => char*
But this is not the case unfortunately
Just now, I tried to ask everyone in my company (they do C++ for living), but not a single one came up with a reasonable answer.
Can someone explain this to me?
Thanks
About the author
#3
so it should specify a type name called "StringCallback", that refers to a pointer to a function taking its arguments and returning (const char *)
12/14/2009 (4:58 pm)
hmmm ,(*)( ) specifies a pointerso it should specify a type name called "StringCallback", that refers to a pointer to a function taking its arguments and returning (const char *)
#4
typedef const char * (*StringCallback) (SimObject *obj, S32 argc, const char *argv[])
instead of
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
Can anyone explain this to me? very please...
Thanks
12/14/2009 (5:02 pm)
if it is a function pointer than the curly brackets should include "StringCallbacka" syntax wise. So, it should have been:typedef const char * (*StringCallback) (SimObject *obj, S32 argc, const char *argv[])
instead of
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
Can anyone explain this to me? very please...
Thanks
#5
I have been thinking about the same thing as you do. Just like you explained, it looks a lot like a function pointer to me, honestly. However, its syntax is not a function pointer.
According to http://www.functionx.com/cpp/keywords/typedef.htm, under the "Type-Defining a Function" section (please scroll down), to type define a function with a funciton pointer, the syntax should be:
typedef int (*func) (int a, int b)
in our case, it should have been
typedef const char* (*StringCallback) (SimObject *obj, S32 argc, const char *argv[])
Notice how the bracket includes the function name
where StringCallback is a pointer to a type of function where
Inputs => SimObject * obj, S32 argc, const char *argv[])
Output => char*
Unfortunately, the bracket does not include StringCallback. Notice that StringCallbrack is our the bracket:
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
So, that is why I think that it is not really a pointer function.
Can anyone explain this to me?
Thanks
12/14/2009 (5:32 pm)
Hi, PicassoI have been thinking about the same thing as you do. Just like you explained, it looks a lot like a function pointer to me, honestly. However, its syntax is not a function pointer.
According to http://www.functionx.com/cpp/keywords/typedef.htm, under the "Type-Defining a Function" section (please scroll down), to type define a function with a funciton pointer, the syntax should be:
typedef int (*func) (int a, int b)
in our case, it should have been
typedef const char* (*StringCallback) (SimObject *obj, S32 argc, const char *argv[])
Notice how the bracket includes the function name
where StringCallback is a pointer to a type of function where
Inputs => SimObject * obj, S32 argc, const char *argv[])
Output => char*
Unfortunately, the bracket does not include StringCallback. Notice that StringCallbrack is our the bracket:
typedef const char *(*) StringCallback (SimObject *obj, S32 argc, const char *argv[])
So, that is why I think that it is not really a pointer function.
Can anyone explain this to me?
Thanks
#6
I'm not sure why they are interchangeable. Though I'm not really surprised - I've already seen some funny bracket use in C++.
Another question is - if they are indeed interchangeable, why were they changed to the format I quoted above?
12/14/2009 (9:46 pm)
The code in 1.0.1+ has it your way, Chin:typedef const char * (*StringCallback)(SimObject *obj, S32 argc, const char *argv[]);
I'm not sure why they are interchangeable. Though I'm not really surprised - I've already seen some funny bracket use in C++.
Another question is - if they are indeed interchangeable, why were they changed to the format I quoted above?
#7
Thank you for letting me know that they have changed the code to what it should be in the current version of 3D Torque
As I mentioned in my first post, I was referring to the TGEA document:
http://docs.torquepowered.com/tgea/official/content/documentation/Engine%20Reference/a04938.html
So, just as you asked, the question is:
“if they are indeed interchangeable, why were they changed to the format I quoted above?”
12/15/2009 (12:58 pm)
Hi, KonradThank you for letting me know that they have changed the code to what it should be in the current version of 3D Torque
As I mentioned in my first post, I was referring to the TGEA document:
http://docs.torquepowered.com/tgea/official/content/documentation/Engine%20Reference/a04938.html
So, just as you asked, the question is:
“if they are indeed interchangeable, why were they changed to the format I quoted above?”
Torque Owner Ivan Mandzhukov
Liman3D
the definition must specify that it is a function pointer