Results 1 to 5 of 5

Thread: public objects / good coding form

  1. #1
    Join Date
    Feb 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default public objects / good coding form

    I'm just getting started again with c++/Qt working on a project.
    I have a class the just lays out a widget with a button and a line edit field.
    I'll be creating an instance of this object in another class (mainwindow), and will need to make some connections with the buttons. Is it bad form to make these buttons public? The below works great, solves my problem, but I would rather do it the right way. Or should I just go about creating signals and slots for these objects and make them private? Thanks for advice


    Qt Code:
    1. class CustomWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit CustomWidget(QWidget *parent = 0);
    6.  
    7. QPushButton *pushButton;
    8. QLineEdit *lineEdit;
    9. private:
    10. QVBoxLayout *vLayout;
    11. QHBoxLayout *hLayout1;
    12. QHBoxLayout *hLayout2;
    13.  
    14. // QLineEdit *lineEdit;
    15. // QPushButton *pushButton;
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: public objects / good coding form

    The idea that some people have is that a widget, like CustomWidget, should hide the implementation details from the user.
    This means that you should use CustomWidget as a single widget without anyone knowing it actually has a push button or a label etc...

    This in turn means that there's a bit of work to do to create useful get/set functions, signals, slots, etc...

    Always let the members of an object be managed by the parent and not by outsiders.
    Take for example the case where you accidentally destroy the pushButton pointer. If you hide those pointers so that nobody can access them, you'll have established a useful protection.

  3. #3
    Join Date
    Feb 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: public objects / good coding form

    thank you for the answer and explanation. makes sense, exactly what i was looking for

    couldn't think of a downside to making them public, thanks for providing one

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: public objects / good coding form

    As a general rule the world outside a class should not know whether certain values are calculated or cached. This means that any access you need should be done through a function. This helps you hide implementation more effectively.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: public objects / good coding form

    This topic, interface design, is a deceptively complex subject. The only rule that you can rely on with certainty is that you must keep the interface as minimal as possible. Once something is public, it is public forever. That is, the public interface of a class is viral.

    As a general rule the world outside a class should not know whether certain values are calculated or cached. This means that any access you need should be done through a function. This helps you hide implementation more effectively.
    Also, lazy initialization is not possible with public access to data members. Pretty much the same type of behavior but wanted to add that anyway. Having Qt class pointers (either directly or by method) in the public interface of the class also really hurts your porting efforts if you ever have to evacuate the Qt framework but that is a topic for another time.

Similar Threads

  1. form and its objects communication
    By eva2002 in forum Qt Programming
    Replies: 9
    Last Post: 31st January 2010, 01:49
  2. Replies: 0
    Last Post: 18th July 2009, 13:07
  3. How can I access public variable parent Form
    By validator in forum Qt Programming
    Replies: 14
    Last Post: 18th December 2008, 21:12
  4. qobject_cast "good coding practice"
    By janus in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2008, 12:02
  5. Replies: 7
    Last Post: 18th July 2006, 21: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.