Results 1 to 2 of 2

Thread: A simple question about setters

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question A simple question about setters

    Hi, I'm programming a class that handles a certain types of parameters. And also can save/load them from disk. Well for each parameter I have it's corresponding setter and getter method all similar to the following:

    Qt Code:
    1. double ProcParamsHandler::sizeH()
    2. {
    3. return m_sizeh;
    4. }
    5.  
    6. void ProcParamsHandler::setSizeH(double newSizeH)
    7. {
    8. if (newSizeH < 0 || newSizeH > MAX_SIZE_H)
    9. newSizeH = INVALID_SIZE;
    10. m_sizeH = newSizeH;
    11. }
    To copy to clipboard, switch view to plain text mode 
    I also have implemented a method to set the value for all the parameters (like the setter above) all in just a simple call. Well my question is: what's the common way to implement this "global" setter: calling its setter for each parameter or putting directly the code of its corresponding setter for each parameter?

    Thanks.

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: A simple question about setters

    It's most elegant to use the setter methods. It prevents code duplication and makes the code easier to read. The performance hit for the test will be negligible.

    Also, I'd throw an exception in case of bad input, not return a 'special' value. It's something you might consider.
    Last edited by Michiel; 13th March 2006 at 10:08.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. simple MVC question!
    By landonmkelsey in forum Qt Programming
    Replies: 14
    Last Post: 19th August 2008, 12:25
  2. Simple RegExp Question...
    By jared.carlson23 in forum Newbie
    Replies: 1
    Last Post: 4th July 2008, 14:10
  3. a simple noob question
    By wrproject in forum Newbie
    Replies: 1
    Last Post: 1st July 2008, 23:25
  4. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  5. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02

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.