Results 1 to 5 of 5

Thread: C++ and default values for functions

  1. #1
    Join Date
    Jan 2006
    Location
    Gloucester, UK
    Posts
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default C++ and default values for functions

    When I learnt a bit about c++ and read about default values for function parameters,
    I thought what a good idea - it was something I always wanted.
    However, I find that they can be a great source of ambiguity when reading other peoples code.
    Does anybody here use default values much or simply avoid them?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and default values for functions

    What kind of ambiguity? I use them all the time and I don't encounter ambiguities... Qt uses default values almost everywhere and you don't hear people complaining about it... Could you elaborate on the problem?

  3. #3
    Join Date
    Jan 2006
    Location
    Gloucester, UK
    Posts
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ and default values for functions

    For example - you are reading some code from someone else,
    you see a funtion called with two parameters - is it a function
    that only takes two parameters only , or is it a function
    that takes more than 2 parameters, but in this case the uses have made use of the defaults?

    Which would you assume?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ and default values for functions

    Quote Originally Posted by guestgulkan View Post
    you see a funtion called with two parameters - is it a function
    that only takes two parameters only , or is it a function
    that takes more than 2 parameters
    But why should I care if takes 2 or more parameters? If it matters, then most likely the code is poorly designed. Default values are not bad, one just might use them in a wrong way, but this doesn't mean you should avoid them.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and default values for functions

    Quote Originally Posted by guestgulkan View Post
    For example - you are reading some code from someone else,
    you see a funtion called with two parameters - is it a function
    that only takes two parameters only , or is it a function
    that takes more than 2 parameters, but in this case the uses have made use of the defaults?
    Default arguments should be used when the functionality they carry is optional. Well designed API should be intuitive, so it should make sense to you to call the function with two parameters. If you needed more functionality out of this function, you'd look at its docs and notice it has additional parameters which tweak the way the function works. Then you can use those parameters to alter the behaviour of the function. Treat a call with default arguments as a base functionality of the function and a call with all arguments used as an extended one.

    As for your question - I don't care how many parameters a function takes. I only care what the function does and I don't have to look at its sources to find out. Well designed APIs are either very intuitive or very well documented (or both).

Similar Threads

  1. Model Drag'n'Drop - Default Move
    By Lemming in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2006, 21:46
  2. Application default font
    By larry104 in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2006, 01:24
  3. Default value for QFont
    By Kapil in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2006, 06:40
  4. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 09:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.