CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2010
    Posts
    9

    variable arguments of different data types

    how to call a function with variable arguments of different data types

  2. #2
    Join Date
    Dec 2009
    Posts
    145

    Re: variable arguments of different data types

    Be more specific please.

  3. #3
    Join Date
    Jun 2010
    Posts
    9

    Re: variable arguments of different data types

    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: variable arguments of different data types

    Quote Originally Posted by t.jalaja View Post
    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
    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.
    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
    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++.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2010
    Posts
    9

    Re: variable arguments of different data types

    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

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: variable arguments of different data types

    Quote Originally Posted by t.jalaja View Post
    Hello Sir
    Thanks for ur reply
    Can u please say how do i accept variable arguments for different data types in managed c++
    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

  7. #7
    Join Date
    Jun 2010
    Posts
    9

    Re: variable arguments of different data types

    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: variable arguments of different data types

    Quote Originally Posted by t.jalaja View Post
    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 )
    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

  9. #9
    Join Date
    Jul 2002
    Posts
    2,543

    Re: variable arguments of different data types

    Use something like:
    void func(array<Object^>^ parameters);

  10. #10
    Join Date
    Jun 2010
    Posts
    9

    Re: variable arguments of different data types

    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

  11. #11
    Join Date
    Aug 2010
    Posts
    51

    Re: variable arguments of different data types

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured