Game Development Community

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


#1
12/14/2009 (4:32 pm)
it is a type defined function
the definition must specify that it is a function pointer
#2
12/14/2009 (4:37 pm)
Sorry, Picasso

Can you please elaborate upon your answer?

thanks
#3
12/14/2009 (4:58 pm)
hmmm ,(*)( ) specifies a pointer
so it should specify a type name called "StringCallback", that refers to a pointer to a function taking its arguments and returning (const char *)
#4
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
12/14/2009 (5:32 pm)
Hi, Picasso

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
#6
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
12/15/2009 (12:58 pm)
Hi, Konrad

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?”
#8
12/16/2009 (7:47 pm)
bump
anyone?