default Arguments

This is a discussion on default Arguments within the C++ Programming forums, part of the General Programming Boards category; for the function having prototype.... void fun(int = 10, int = 20, int = 30, int = 40); how to ...

  1. #1
    Registered User kishore's Avatar
    Join Date
    Jan 2007
    Location
    bangalore
    Posts
    6

    Question default Arguments

    for the function having prototype....

    void fun(int = 10, int = 20, int = 30, int = 40);

    how to make sure that when it is called with two arguments
    such as.....

    fun(1, 2);

    these two are treated as the 1st & 3rd arguments
    while the 2nd & 4th take the default values???????????

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Not (currently) possible. Try to search before you ask, there is a recent thread on the topic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    It is technically possible to achieve the effect by overloading, but that limits the ability to use default arguments.
    Code:
    void fun(int, int, int, int = 40)
    {
         // whatever
    }
    
    void fun(int a, int b)
    {
        fun(a, 20, b);
    }
    
    void fun(int a = 10)
    {
        fun(a, 20, 30, 40);
    }
    This isn't a particularly good idea; at the least it will make it VERY easy for a user of these functions to make an error, and get the arguments in a wrong order. So there would be a maintenance nightmare associated with the any function that calls fun().

    IMHO, any coder who uses this sort of technique in production code that someone else has to reuse should be summarily shot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 02:43 PM
  2. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 07:27 PM
  3. Default Arguments Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2005, 06:27 PM
  4. Default Arguments
    By pianorain in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 08:40 AM
  5. pointers as default arguments
    By ajm in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21