Thread: Variable function argument types

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Variable function argument types

    I was wondering if one could write a function that could accept one or the other variable type.

    ex: I have 2 arrays, int** and double**, and a function
    Code:
    void PGMWrite(double** Matrix, int Matrix_dimension){.....}
    is there any way to change the function to
    Code:
    void PGMWrite(int** Matrix || double** Matrix, int Matrix_dimension){.....}
    and then have some sort of type identifier in the function that picks the correct section via an if loop? If so how, and how would I identify in the function if the input it type double or int?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does PGMWrite do? Show us its implementation with say an int** first parameter.

    One approach might be to use a pointer to void then cast to the appropriate type based on say, an enum. Or, you might just have two different functions. Or you might have two parameters, then pass a null pointer to leave the parameter unused. Or, a macro might work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by laserlight View Post
    Or, you might just have two different functions.
    This is a common solution for this problem. The function with the fewer number of parameters calls the other function with more parameters, passing reasonable default parameters for the ones not input by the function with the fewer number of parameters. The function that takes a larger number of parameters could have some if statements to recognize that it was called by the other function. This is sometimes called a helper function.

    C++ Helper Functions | Talk Binary
    Last edited by rcgldr; 06-12-2013 at 09:49 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You can't generally get parametric polymorphisms in portable, standard C; you can do some stuff with macros if your compiler has some flavor of `typeof', but that's abnormal, and I don't recommend it.

    I'd recommend using inheritance polymorphisms, the C flavor, if you want to use the same function name.

    Soma

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you all,

    Having 2 different functions, one for int** and one for double** is a but bulky as the function would be exactly the same simply with a cast. So I think that rcgldr's follow up on laserlight's comment is a good one namely creating a function that takes either
    int** or double** with a null and then calls the approperiate cast for PGMwrite() or whatever the function might be.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Argument inside the Argument of a Function Declaration
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2011, 05:53 AM
  2. variable value not assignment to function argument
    By Almsoo7 in forum C Programming
    Replies: 2
    Last Post: 02-28-2010, 12:06 PM
  3. Command line argument types
    By radnik in forum C++ Programming
    Replies: 10
    Last Post: 06-30-2008, 04:58 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. What argument for a function? for static array variable
    By liechmaster in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 05:11 PM

Tags for this Thread