Default parameter

This is a discussion on Default parameter within the C++ Programming forums, part of the General Programming Boards category; Can a function have more than one default parameter? Like maybe: Code: class A { public: void methodA(int test, int ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Default parameter

    Can a function have more than one default parameter? Like maybe:

    Code:
    class A
    {
      public:
        void methodA(int test, int test2=0, bool state=true);
    };
    
    void A::methodA(int test, int test2, bool state)
    {
    }
    If it is possible, how do I use only one of them (e.g. from the code above I just used the default test2=0 and set the state to false)?

    Thanks in advance.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    I would imagine that would cause ambiguity. I'd just put in the default explicitly for the middle argument. Perhaps for the same reason that
    >>When a default is provided, all remaining parameters must be given defaults. There can not be any holes.

    Similarly, once a default is "used" in a call, all others must be default as well.

    However, I am entirely unsure. You'd think that type-checking might make this possible, but if they're the same type.. if an implicit cast would work... I don't think you can do it.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I believe they are considering adding to the next standard the ability to specify which function parameter an argument refers to. I don't remember if this is only for template arguments or for function arguments, though. Until then, if you want to use the default value for one parameter, you have to use the default for all the rest after it. And if you don't want to use the default for a parameter, you have to specify a value for all the rest before it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-18-2009, 04:58 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 03:21 PM

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