Results 1 to 8 of 8

Thread: Clean way to keep all private variables in class ( private struct, pimpl idiom )

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Clean way to keep all private variables in class ( private struct, pimpl idiom )

    Hi, I would ask, how you keep private variables in the class? If there are a few then it is not a problem but now I have already over a dozen variables in class and I look for some way to do it more "clean".

    Now I use private structure to storage all my private variables, for example

    Qt Code:
    1. class CustomClass
    2. {
    3. struct Data {
    4. QTableWidgetItem * hoverItem;
    5. QLabel label;
    6. QString text;
    7. QAction file;
    8. QAction save;
    9. // other variables
    10. };
    11.  
    12. public:
    13. CustomClass();
    14. ~CustomClass();
    15.  
    16. private:
    17. Data data;
    18. };
    To copy to clipboard, switch view to plain text mode 

    I also read about pimpl Idiom
    http://qt-project.org/wiki/Dpointer.

    How you are doing it in daily work?
    Thanks,

  2. #2
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    pimpl is only really needed if you need platform-independent data or wanting to hide a lib data.
    if you don't need to hide data, just stay with private data.

  3. The following user says thank you to Alundra for this useful post:

    atomic (3rd March 2015)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    Hi, I would ask, how you keep private variables in the class?
    It will sound silly but it is the fact. You have to declare under the "private:" section in the class.

    If there are a few then it is not a problem but now I have already over a dozen variables in class and I look for some way to do it more "clean".
    I think you should try to have more organized code.

    Now I use private structure to storage all my private variables, for example
    You just pushed your "non-clean" private variables to another structure, IMO now the structure is "not-clean".

    I also read about pimpl Idiom
    By using private implementation, you will me moving the all the variable (non-clean) variables from *.h to *.cpp file. IMO now the *.cpp file is not clean.

    How you are doing it in daily work?
    I personally give attention to organizing the classes, structs by their functionalities. In a case there are more private variables (more than 15/20) then I consider that the class / struct is taking too much functionality it be better have some nested classes/struct to manage smaller functions or if any of these smaller functions are of general in nature then split the class itself. Make is just a guideline not rule to your code.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    atomic (3rd March 2015)

  6. #4
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    Maybe I wrong name this post, I thought about a large number of private variables as you say > 15.
    Sometimes this "data" structure can eliminated conflict of names and you do not need create prefix to variables like _m or similar.
    Thanks,

  7. #5
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    Coding rules are important, and that's important to keep these rules during all the development.
    For class member, keep the "m_" or just "m" front of variables.
    Use "Set", "Get", "Count" and "Compute" in function's names.

  8. The following user says thank you to Alundra for this useful post:

    Kryzon (4th March 2015)

  9. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    pimpl is only really needed if you need platform-independent data or wanting to hide a lib data.
    Or you want to shorten the rebuild time (change in the class internals will require to rebuild only the class.cpp file). Very useful in large projects IMHO.

  10. #7
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    Quote Originally Posted by atomic View Post
    Hi, I would ask, how you keep private variables in the class? If there are a few then it is not a problem but now I have already over a dozen variables in class and I look for some way to do it more "clean".
    I just resort to proper naming, commenting and using whitespace to separate members into logical blocks.

    Qt Code:
    1. class CustomClass
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CustomClass();
    7. ~CustomClass();
    8.  
    9. private:
    10. // Widgets.
    11.  
    12. QTableWidget* m_mainTable;
    13. QTableWidgetItem* m_hoverItem;
    14. QLabel m_label;
    15.  
    16. // QActions.
    17.  
    18. QAction m_fileOpen;
    19. QAction m_fileSave;
    20. QAction m_fileClose;
    21.  
    22. // Data.
    23.  
    24. QString m_text;
    25. QString m_lastFile;
    26. };
    To copy to clipboard, switch view to plain text mode 
    Do you have name conflicts right now? If you're just planning ahead to avoid it, it might be a case of you being overzealous:
    http://c2.com/cgi/wiki?YouArentGonnaNeedIt

    Although its not a formal source and you have to be careful with what you look at, I sometimes like to reference the source code of other Qt applications to look for stylistic choices and patterns: http://qt-apps.org/

  11. #8
    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: Clean way to keep all private variables in class ( private struct, pimpl idiom )

    For complex cases I use pimpl-like solution, for simple ones I use private members directly. I don't consider it cluttering the file. Pimpl is also fine if you want to hide the internal representation of the object when implementing a library.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 5th April 2012, 12:38
  2. Segfault while using Pimpl idiom with Qt types.
    By darkadept in forum Qt Programming
    Replies: 11
    Last Post: 13th June 2009, 00:53
  3. Do you use Pimpl Idiom?
    By ComaWhite in forum General Discussion
    Replies: 5
    Last Post: 25th March 2009, 09:48
  4. Omitting private part of a class definition
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 23rd March 2007, 11:51
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.