code_junkie

Is it a good (correct) way to encapsulate a collection?

Nov 11th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. class MyContainedClass {
  2. };
  3.  
  4. class MyClass {
  5. public:
  6. MyContainedClass * getElement() {
  7. // ...
  8. std::list<MyContainedClass>::iterator it = ... // retrieve somehow
  9. return &(*it);
  10. }
  11. // other methods
  12. private:
  13. std::list<MyContainedClass> m_contained;
  14. };
  15.  
  16. const MyContainedClass * getElement() const {
  17. // ...
  18. std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
  19. return &(*it);
  20. }
  21.  
  22. const MyContainedClass & getElement() const {
  23. // ...
  24. std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
  25. return *it;
  26. }
  27.  
  28. std::list<MyContainedClass>::const_iterator getElement() const {
  29. // ...
  30. std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
  31. return it;
  32. }
  33.  
  34. const MyContainedClass * getElement() const {
  35. // ...
  36. std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
  37. return &(*it);
  38. }
  39.  
  40. const MyContainedClass& getElement() const {
  41. // ...
  42. std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
  43. return *it;
  44. }
  45.  
  46. class Paragraph {
  47. public:
  48. // getters and setters for private fields
  49. private:
  50. FONTFACET m_fontFace;
  51. FONTSIZET m_fontSize;
  52. CONTENTT m_content;
  53. }
  54.  
  55. class Document {
  56. public:
  57. Paragraph * NewParagraph();
  58. void DeleteParagraph(Paragraph * p);
  59. Paragraph * GetParagraph(/*parameters to select particular paragraph*/) {
  60. std::list<Paragraph>::iterator it = // retrieve according to arguments
  61. return &(*it);
  62. }
  63. // getters and setters of document's properties
  64. private:
  65. std::list<Paragraph> m_paragraphs;
  66. // other properties of Document
  67. }
  68.  
  69. class ToolbarParagraphEditor: public ToolbarWidget {
  70. public:
  71. // implementation.
  72. private:
  73. Paragraph * m_paragraph;
  74. }
  75.  
  76. class DialogParagraphEditor: public DialogWidget {
  77. public:
  78. // implementation
  79. private:
  80. Paragraph * m_paragraph;
  81. }
  82.  
  83. class ParagraphSelectionDialog: public DialogWidget {
  84. // implementation
  85. private:
  86. std::list<Paragraph *> m_selectedParagraphs; // Selection dialog doesn't own paragraphs
  87. }
Add Comment
Please, Sign In to add comment