how to call a function with variable arguments of different data types
Printable View
how to call a function with variable arguments of different data types
Be more specific please.
i have a function with variable arguments of different data types
eg: func(const1,int,const2,double,const3,string,NULL)
such func, i used va_list macros in native c++successfully using switch ... case, for conts1 assigning int, for const2 , double etc
but in managed c++ how do i
In sample programs i got know how to accept variable arguments only for integers not for mixed data types, so i want to know whether i can use va_list etc macros in managed c++, i tried using but it gives error like calling native code in managed
jalaja
First of all, if one of your types was really "string" as in "std::string", then what you did is ill-formed and leads to undefined behaviour. It is invalid to pass non-POD type as a varying argument.
So just that alone means your "solution" (if what you posted is what you did), was no solution to begin with.
I suggest you redesign your program to not do this, as pointed out, you can't pass non-POD types as varying parameters, regardless of what results you seem to have gotten with doing it in C++.Quote:
In sample programs i got know how to accept variable arguments only for integers not for mixed data types, so i want to know whether i can use va_list etc macros in managed c++, i tried using but it gives error like calling native code in managed
jalaja
Regards,
Paul McKenzie
Hello Sir
Thanks for ur reply
Can u please say how do i accept variable arguments for different data types in managed c++
for eg: func(...array<int>args) is for integers , in the same way how for strings, double, etc in the same function. If not able to understand my problem in specific, can u pl explain the variable arguments sample program other than for integers. Since theis topic is explained in many places for integers alone
waiting for an early response
regards
jalaja
First, explain on a high level what you're trying to achieve, instead of telling us what your "solution" is, and trying to make your solution work. Don't say "I'm passing varying arguments" -- explain what functionality you are attempting to achieve, not how you achieved it.
Second, C++ has templates. They are overloaded by type. If you need to do something generic, then write a template class/function, where the type is one of the template arguments.
Regards,
Paul McKenzie
Thank u Sir
iam porting code from c++ to managed c++ .net. In my c++ i have a function named
func(const1, datatype1,const2,datatype2,and so on with variable arguments) declaration in header file in a class and implementation of this in cpp file (using macros from stdarg.h ) Thro application program iam calling the same func. But if i use macros from cstdarg in managed c++ .net, it gives a compilation error
hope iam clear this
regards
jalaja
I know this already, you've stated this three times now, and I understand. That is not what I'm asking you.
What I'm asking you is what is the purpose of this function? What is it trying to achieve?
Sometimes, you can't do line-by-line translations from one language to another. You have to understand exactly what the purpose of that function was, and if the other language doesn't have such a mechanism, rewrite that function using different techniques and paradigms.
Regards,
Paul McKenzie
Use something like:
void func(array<Object^>^ parameters);
Sorry for the delayed response
i tried using the array<Object>, so with this type iam able to pass integers, float, string, double (with appropriate tryParse)but if i pass struct, it gives compilation error of type difference,how do i typecast object to struct
regards
jalaja
hi,
i have a function with variable arguments of different data types
eg: func(const1,int,const2,double,const3,string,NULL)
such func, i used va_list macros in native c++successfully using switch ... case, for conts1 assigning int, for const2 , double etc
regards,
phe9oxis,
http://www.guidebuddha.com