write a function such that when ever i call this function in some other function .it should give me

Discussion in 'C++' started by komal, Jan 25, 2005.

  1. komal

    komal Guest

    hi all
    basically my problem is i have to write a function such that when ever
    i call this function in some other function .it should give me tha data
    type and value of calling function parameter.and no of parameter is
    calling function can be anything.
    for example.suppose my function is function2.
    then when i call
    function1(int i ,char j,float d)
    {
    function2()
    }

    ouput should be
    i is integer
    j is char
    d is float

    and data type can be anything
     
    komal, Jan 25, 2005
    #1
    1. Advertising

  2. On 24 Jan 2005 20:47:35 -0800, "komal" <>
    wrote:

    >hi all
    >basically my problem is i have to write a function such that when ever
    >i call this function in some other function .it should give me tha data
    >type and value of calling function parameter.and no of parameter is
    >calling function can be anything.
    >for example.suppose my function is function2.
    >then when i call
    >function1(int i ,char j,float d)
    >{
    >function2()
    >}
    >
    >ouput should be
    >i is integer
    >j is char
    >d is float
    >
    >and data type can be anything


    I question the wisdom or usefulness of doing this. Sounds like a
    homework assignment to me.

    Anyway, you can try using the ellipsis and va_arg, but there are
    certain restrictions as to the types of arguments you can pass. Most
    importantly, the standard says that invoking va_arg on non-POD class
    types results in undefined behavior (section 5.2.2 part 7). To use
    va_arg, you need to include <cstdarg>.

    Also, check out the std::type_info class for non-POD class RTTI.
    Include the header <typeinfo> to use this class.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jan 25, 2005
    #2
    1. Advertising

  3. komal

    msalters Guest

    komal wrote:
    > hi all
    > basically my problem is i have to write a function such that when

    ever
    > i call this function in some other function .it should give me tha

    data
    > type and value of calling function parameter.and no of parameter is
    > calling function can be anything.
    > for example.suppose my function is function2.
    > then when i call
    > function1(int i ,char j,float d)
    > {
    > function2()
    > }
    >
    > ouput should be
    > i is integer
    > j is char
    > d is float


    Impossible. function2 doesn't get any information about function1.
    At the very least, you should write it as

    f1(int i ,char j,float d) { f2( &f1 ); }

    In that case, f2 can be a template. With Template Argument Deduction
    you can then at least know the types. Still, the variable names are
    only for the compiler and not available at runtime.
    Regards,
    Michiel Salters
     
    msalters, Jan 25, 2005
    #3
  4. komal

    msalters Guest

    komal wrote:
    > hi all
    > basically my problem is i have to write a function such that when

    ever
    > i call this function in some other function .it should give me tha

    data
    > type and value of calling function parameter.and no of parameter is
    > calling function can be anything.
    > for example.suppose my function is function2.
    > then when i call
    > function1(int i ,char j,float d)
    > {
    > function2()
    > }
    >
    > ouput should be
    > i is integer
    > j is char
    > d is float


    Impossible. function2 doesn't get any information about function1.
    At the very least, you should write it as

    f1(int i ,char j,float d) { f2( &f1 ); }

    In that case, f2 can be a template. With Template Argument Deduction
    you can then at least know the types. Still, the variable names are
    only for the compiler and not available at runtime.
    Regards,
    Michiel Salters
     
    msalters, Jan 25, 2005
    #4
  5. Re: write a function such that when ever i call this function in someother function .it should give me tha data type and value of callingfunction parameter

    Bob Hairgrove wrote:
    >
    > On 24 Jan 2005 20:47:35 -0800, "komal" <>
    > wrote:
    >
    > >hi all
    > >basically my problem is i have to write a function such that when ever
    > >i call this function in some other function .it should give me tha data
    > >type and value of calling function parameter.and no of parameter is
    > >calling function can be anything.
    > >for example.suppose my function is function2.
    > >then when i call
    > >function1(int i ,char j,float d)
    > >{
    > >function2()
    > >}
    > >
    > >ouput should be
    > >i is integer
    > >j is char
    > >d is float
    > >
    > >and data type can be anything

    >
    > I question the wisdom or usefulness of doing this. Sounds like a
    > homework assignment to me.
    >
    > Anyway, you can try using the ellipsis and va_arg, but there are
    > certain restrictions as to the types of arguments you can pass. Most
    > importantly, the standard says that invoking va_arg on non-POD class
    > types results in undefined behavior (section 5.2.2 part 7). To use
    > va_arg, you need to include <cstdarg>.


    And it wouldn't help anyway.
    When working with va_arg you already need to know the type of the
    passed arguments. That's one reason why printf has those fancy %d, %f
    &c, %s, ... formatting flags.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 25, 2005
    #5
  6. komal

    msalters Guest

    komal wrote:
    > hi all
    > basically my problem is i have to write a function such that when

    ever
    > i call this function in some other function .it should give me tha

    data
    > type and value of calling function parameter.and no of parameter is
    > calling function can be anything.
    > for example.suppose my function is function2.
    > then when i call
    > function1(int i ,char j,float d)
    > {
    > function2()
    > }
    >
    > ouput should be
    > i is integer
    > j is char
    > d is float


    Impossible. function2 doesn't get any information about function1.
    At the very least, you should write it as

    f1(int i ,char j,float d) { f2( &f1 ); }

    In that case, f2 can be a template. With Template Argument Deduction
    you can then at least know the types. Still, the variable names are
    only for the compiler and not available at runtime.
    Regards,
    Michiel Salters
     
    msalters, Jan 25, 2005
    #6
  7. komal

    msalters Guest

    komal wrote:
    > hi all
    > basically my problem is i have to write a function such that when

    ever
    > i call this function in some other function .it should give me tha

    data
    > type and value of calling function parameter.and no of parameter is
    > calling function can be anything.
    > for example.suppose my function is function2.
    > then when i call
    > function1(int i ,char j,float d)
    > {
    > function2()
    > }
    >
    > ouput should be
    > i is integer
    > j is char
    > d is float


    Impossible. function2 doesn't get any information about function1.
    At the very least, you should write it as

    f1(int i ,char j,float d) { f2( &f1, i,j,d ); }

    In that case, f2 can be a template. With Template Argument Deduction
    you can then at least know the types. Still, the variable names are
    only for the compiler and not available at runtime.
    Regards,
    Michiel Salters
     
    msalters, Jan 25, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Pope
    Replies:
    5
    Views:
    464
    Silvio Bierman
    Feb 22, 2004
  2. yuyazhang
    Replies:
    14
    Views:
    717
    yuyazhang
    Apr 29, 2006
  3. grocery_stocker
    Replies:
    10
    Views:
    629
    Keith Thompson
    May 25, 2005
  4. Jason
    Replies:
    0
    Views:
    202
    Jason
    Jul 6, 2004
  5. Bing Du
    Replies:
    1
    Views:
    137
    James Willmore
    Aug 29, 2003
Loading...

Share This Page