Skip to main content
gamedev.net
🔒 Locked

typedef question

Started by nuclear123 Sep 9, 2010 at 6:58 AM 5 replies 1.3k views
Original Post
nuclear123
nuclear123
im curious as to what this means exactly

typedef void ( *BG_EvaluateTrajectory_Prototype )( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result );

i can substitute ( *BG_EvaluateTrajectory_Prototype in every where i would normally use ( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result )?
grekster
grekster
if you mean that instead of writing

void EvaluateTrajectory( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result );

you could write

void EvaluateTrajectory(BG_EvaluateTrajectory_Prototype);

Then the answer is no.

The typedef you have written out there defines a function pointer "type" to a function that returns nothing (void) and takes those paramaters listed.
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
nuclear123
nuclear123
oh ok thanks...so in other words..

typedef void ( *BG_EvaluateTrajectory_Prototype )( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result );

( *BG_EvaluateTrajectory_Prototype ) is a pointer to a function that returns void and has the parameters ( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result )

but now my question is how does typedef effect how i use this pointer to a function???
alvaro
alvaro
Quote:
Original post by nuclear123
oh ok thanks...so in other words..

typedef void ( *BG_EvaluateTrajectory_Prototype )( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result );

( *BG_EvaluateTrajectory_Prototype ) is a pointer to a function that returns void and has the parameters ( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result )

but now my question is how does typedef effect how i use this pointer to a function???


Using typedef in this fashion can make your code much more readable. For instance, if you write a function that takes a callback of this type as a parameter, you can declare it this way:

some_return_type some_funcion(BG_EvaluateTrajectory_Prototype evaluate_trajectory);


I would probably have used a different name for the type, since "evaluate" tells me that the function should return some "evaluation", not void. But that's the general idea.

One case where I have used a typedef of this style is in passing a comparison function to qsort (before I learned C++ and how to use std::sort):
#include <iostream>#include <cstdlib>typedef int (*Comparison)(const void *a, const void *b);int ascending(const int *a, const int *b) {  return *a - *b;}int main() {  int array[] = {1,5,3,7,2,3,4,6,9,2,5,1,0};  int n = sizeof(array)/sizeof(*array);  std::qsort(array, n, sizeof(*array), (Comparison) ascending);  for (int i=0; i<n; ++i)    std::cout << array << '\n';}
nuclear123
nuclear123
example would be
instead of
-----------------------------------------------------------------
void myfunction(BG_EvaluateTrajectory_Prototype( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result));

void myfunction(BG_EvaluateTrajectory_Prototype evaluate_trajectory);

2nd means same thing is typedef was made, and is alot easier to read!?
alvaro
alvaro
Quote:
Original post by nuclear123
example would be
instead of
-----------------------------------------------------------------
void myfunction(BG_EvaluateTrajectory_Prototype( trajectory_t *tr, int atTime, vec3_t *base, vec3_t *result));

void myfunction(BG_EvaluateTrajectory_Prototype evaluate_trajectory);

2nd means same thing is typedef was made, and is alot easier to read!?


The first one doesn't work. You should try these things using a compiler and a 10-line test program: It's the best way to test your understanding of the language.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.