does ANYONE who knows how to do this???

Started by
1 comment, last by jritts 23 years, 11 months ago
This post is about functions with optional arguments (ANSI compatible). I have a function that looks something like this: void AddWaypoints(int speed, int numWayPts, int x1, int y1, ...); And I want to make this function: void AddWalkingWaypoints(int numWayPts, int x1, int y1, ...); It''s the same thing, only you don''t have to give it a travelling speed (it''ll use some predefined constant instead.) I just want to be able to pass the list of points sent to AddWalkingWaypoints to AddWaypoints. How do I do that? I think printf passes its optional args to vprintf in a similar way, but i''m not sure. I tried to do this within AddWalkingWaypoints - { va_list params; va_start(params, y1); AddWaypoints(CH_MOVEMENT_WALKINGSPEED, numPts, x1, y1, params); va_end( params ); return 0; } and tried dereferencing params in the call to AddWaypoints, but it doesn''t work. Can someone help me? Thanks in advance! -James
I''m fluent in C++ and Java, know something of Perl, HTML, DirectDraw.CSE student at the University of California San Diego.
Advertisement
First a dumb question. Are you needing to do this in ANSI C, not C++? In C++ there are actually four solutions, but one of the is WAY harder than the others, and another one is not as clean as the others, and THOSE two are the ONLY ones availible to C programmers. Here''s the list:

(recommended C++)
1. A function with a default parameter.
(just as good C++)
2. An overloaded function.

(not clean but works in C)
3. Simply make two functions (they need different names, but you can implement the one that takes one less parameter as simply a call to the larger function that uses the predefined constant.
(hard and inefficient, but functional, for C)
4. Variable argument list (like printf).

Good luck, and feel free to write back with questions for more details about any of the above, or other ideas.
i''m not sure how you could use either a default argument or an overloaded function, since your types are integer and there would be no way for the compiler to know whether the parameters were actually part of the ... of one function or not.. hope that makes sense

you might pass an array of waypoints, that would actually be more useful because you could change the size at runtime.. instead of everything being pre-specified in the call.

or like Xai said, you could use a variable list parameter,
but it wouldnt be as flexible as an array.
like this..

void AddWayPoints(int speed, int num, int x, int y, ...);void AddWalkingWaypoints(int num, int x, int y, ...);void AddWayPoints_(int speed, int num, int x, int y, va_list list);void AddWayPoints(int speed, int num, int x, int y, ...){  va_list list;  va_start(list, y);  AddWayPoints_(speed, num, x, y, list);  va_end(list);}void AddWalkingWaypoints(int num, int x, int y, ...){  va_list list;  va_start(list, y);  AddWayPoints_(100, num, x, y, list);  va_end(list);}then implement AddWayPoints_() however you do that..well i hope this helps.. good luck!    


adamm@san.rr.com
adamm@san.rr.com

This topic is closed to new replies.

Advertisement