Results 1 to 4 of 4

Thread: Class Definition in cpp file

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Class Definition in cpp file

    I am putting my declaration and implementation of "private classes" (only the containing cpp files can instantiate it) in my the cpp files like below
    Qt Code:
    1. //in my header file
    2.  
    3. class MyMainClass
    4. {
    5. public:
    6. int someStuff();
    7. private:
    8. };
    9.  
    10. //in my cpp file
    11.  
    12. class MyPrivateClass
    13. {
    14. public:
    15. int getSomeStuff()
    16. {
    17.  
    18. int the_result = // do some stuff and return the result
    19. return the_result;
    20. }
    21. };
    22.  
    23. int MyMainClass::someStuff()
    24. {
    25. MyPrivateClass p;
    26. return p.getSomeStuff() + 5;
    27. }
    To copy to clipboard, switch view to plain text mode 

    is this considered bad practice? as alleged by some of my co-worker.

    My reasoning behind doing this is that i don't want to share my "MyPrivateClass" so i did not create a header and cpp file for it. My concern is that they might start using my "MyPrivateClass" which is not designed to be shared if i will create another files for it.

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class Definition in cpp file

    I think the problem is not that you'll have more headers and source files , i think it's a pure design problem : If MyPrivateClass's by is not supposed to interact only with certain types : it's interface must completely reflect this. Of course it depends on how exactly MyMainClass and MyPrivateClass , and it's child classes are being used , but yeah , sharing one source entity between two classes does look kinda suspicious.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class Definition in cpp file

    What you are doing is essentially a version of the pimpl idiom, which seems fine.

    e.g.
    http://www.gamedev.net/page/resource...-c-pimpl-r1794
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class Definition in cpp file

    I will Always do this when I am coding a unit test where mocked classes are written in my cpp files
    I don't think this is a bad practice

Similar Threads

  1. Replies: 3
    Last Post: 8th August 2010, 10:53
  2. Replies: 3
    Last Post: 27th December 2008, 19:34
  3. Avi File Class
    By ascii in forum Qt Programming
    Replies: 4
    Last Post: 12th November 2008, 11:34
  4. Replies: 3
    Last Post: 19th February 2008, 13:10
  5. Omitting private part of a class definition
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 23rd March 2007, 11:51

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.