Looking for a more effecient way to pass arguments to functions.
Lately my functions for my game have incredibly bloated with parameters, example:
int move_xy(int xM, int yM, int xL, int yL, int *xB, int *yB, int *xBL, int *yBL) ... what would be a more effecient way of sending information to a function? |
Passing a struct?
|
Quote:
:D |
hey
best way t pass pointersonly or struct |
Well I used to use all ponters, but I am changing a good hundred more or less lines of code so that I can have all of them function within one, and it doesn't seem apparent to me that I need all pointers passed. Anyways, I'll look into passing some sort of struct (which I believe is a class but instead of everything defaulting to private, it is public, and is somehow different in c++ then in c, correct?).
|
Hi -
1. For your purposes, a "class" with nothing but data, and all public members, is completely equivalent to a struct: Code:
#include <stdio.h> Quote:
Quote:
Quote:
BOTTOM LINE: If you're only passing data, I'd just define a struct and be done with it. If you're defining methods with respect to that data, I'd define a class. And if you're going to define a class, then I'd treat it like a class (appropriate constructors, copy constructors; declare the destructor "virtual" if you're going to subclass; getter and setter functions; *NO* public visibility to member data: basically, the whole nine yards). 'Hope that helps .. PSM |
You could pass a pointer to a union of structs.
To clarify, each of your function parameter lists will be more or less unique to that function. You could, in theory, define a struct for each separate function parameter list. Then, create a union of them. Declare a variable of the union, and set the appropriate fields prior to each call. I can give an example if it's not clear... it's hard to explain. Or, you could do a hybrid. Most of your functions will need access to a "base" set of variables. Say, for instance, a handle to the screen's context, or the position of an object on a virtual map. You can create a struct containing those "base" variables, create a union of structs for the individual function requirements, and then create a wrapper struct to contain both. Ok, I can't help myself... example: Code:
typedef struct primary_parameters EDIT: Forgot the "_handle" when referencing "log_file_handle" |
Quote:
If your serious about making games maybe it would be an idea to create vector classes/structs such as Code:
class Vector_2d For the likes of ...int *xB.... maybe you should protect the data like: int const * const xB |
Quote:
I learned about constants recently and I have made a note to myself now to use it more often (when needed), so I don't get some mistake such as the one above. Now I'm off to learn vectors. Thanks everyone! BTW: If you would like to play with it (please don't send any corrections back to me IF anyone even decides to mess with it), here is a link the most recent working version: http://pntbalg.shackspace.com/GeorgeLair/042606. Just copy everything you see in some folder go into a console and cd into the directory containing these and type make. |
[QUOTE=Dark_Helmet]You could pass a pointer to a union of structs.
Nice solution. But i think the issue here is to reduce the load of parameters being passed. In your solution you eventually passed a *pointer* to the union, which requires the same resources on the stack if you would've passed a pointer to structure, class, variable etc. To me the most efficient approach in this scenario is to simply pass a pointer to a container object (union, structure, class, array). |
Quote:
However, passing a pointer to a struct (or similar) makes sense. In that case only 4 bytes (8 in 64bit systems) have to be copied to the stack when the function is called. When you use the struct directly, it would still have been the full content that will be copied on the stack. |
All times are GMT -5. The time now is 06:15 PM. |