C++ question: function with variable number of args [SOLVED]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

C++ question: function with variable number of args [SOLVED]

Post by evo »

Hi,

In C/C++ it is possible to create a function that takes a variable number of arguments using the va_list type found in stdarg.h. These arguments can be used as input for the function.

Does anybody know if it is possible to create a function where a variable number of arguments can be used as output. So that each of the arguments gets a new value assigned.
I would like to create something like this:

Code: Select all

void   CopyResult(int numberArgs, ...) {
   va_list	argp;
   va_start(argp, numberArgs);
   for(int i = 1; i < numberArgs; i++)  {
      //assign argument 'i' here
      .....
      va_arg(argp, int);  }
   va_end(argp);
   }
}
Last edited by evo on Tue Sep 27, 2005 7:19 pm, edited 1 time in total.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

may be u culd use an array

but i am wrong since id ont understand the code
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

An array is not possible for me. Since I plan to extend the function to not only take a variable number of arguments but also variable types.

I want to call the function like this:

Code: Select all

int ID;
char[100] StrValue;
int       IntValue;

CopyResult(2, ID, StrValue);

// OR

CopyResult(2, ID, IntValue);
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

may be a list?
sRc
Posts: 431
Joined: Thu Jul 28, 2005 1:44 am
Location: Salt Lake City, Utah
Contact:

Post by sRc »

just overload the functions:

http://cplus.about.com/od/beginnerctuto ... 61602c.htm

EDIT: or make the function a void pointer:

http://www.cplusplus.com/doc/tutorial/tut3-3.html (near the bottom)
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

Overloading is not quit the solution I think. I'll show some better examples:

CopyResult(6, Int1, Int2, Str1, Double1, Str1, Int3);

or

CopyResult(3, Int1, Double1, Str1);


The function I need has to be completely variable in it's parameters.
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post by MindGames »

Well you could pass a std::vector<void *> or std::list<VARIANT> which would allow it to sort of work. To me this is not a good solution though. What are you trying to achieve? From the sounds of it, one function to do all does not seem like a terribly good design, though obviously I have not got enough information to judge.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

why not return a class ?
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

Hi,

I just cracked the problem. With a little help from the guys from: http://cboard.cprogramming.com
I can thoroughly recommend this forum if you ever have a C/C++/C# question. Very active forum, quick and to the point replies.

Not that I don't appreciate or don't trust all your help :wink:

And this is the solution (which makes me appreciate C++ even more):

Code: Select all

bool	SQL_ResultWithValues(SQL_Output *SQL_Statement, const char *Typelist, ...) 
{
	int     cType, Column;
	char   *TString = 0;
	int    *TInt    = 0;
	double *TDouble = 0;
	va_list	argp;
	
	va_start(argp, Typelist);
	for (int i = 1; i < strlen(Typelist); i += 2)
	{
		Column = (int)((i-1)/2);
		if (!strncmp(Typelist + i - 1, "%", 1 ) )
		{
			cType = (int)Typelist[i];
			switch (cType)
			{
			case 's' : case 'S'	:
				TString = va_arg(argp, char*);
				strcpy(TString, getResultAsString(SQL_Statement, Column) );
			break;
			case 'd' : case 'D'	:
				TInt	= va_arg(argp, int*);
				*TInt	= getResultAsInt(SQL_Statement, Column);
			break;
				TDouble = va_arg(argp, double*);									
				*TDouble = getResultAsDouble(SQL_Statement, Column);
			break;
			default				:
				printf("Unknown Type in Typelist: %s at position %d Token=%s\n", Typelist, i, Typelist[i]);
				va_end(argp);
				return false;
			break;
			}
		}
		else
		{	printf("Error parsing Typelist: %s at position %d\n", Typelist, i);
			return false;
		}
	}
	va_end(argp);
	return true;
}
I am calling this function for example with:

Code: Select all

int    ID;
double Value;
char   Remark[100];

DB->SQL_ResultWithValues(&SQLOut, "%d%f%s", &ID, &Value, &Remark);

printf("%d,%f,%s\n", ID, Value, Remark );
You can propably now guess what I am working on. I'll release this stuff in a couple of weeks, after a lot more testing. I promise you that the results will be awesome. 8)
Post Reply