0

Suppose the following DTO class. Which of the two getters breaks encapsulation least?

class Foo
    {
    public:
        /*Return the most primitive type. Caller do not need
        to worry about what Array is*/
        const RecType* dataBeginGet() const;
        const RecType* dataEndGet() const;

        /*Return a reference to the Array.
         Caller do not need to call two methods before iteration
        */
        const Array<RecType>& dataGet() const
            {return m_data;}

    private:
        Array<RecType> m_data;
    };
gnat
  • 21,442
  • 29
  • 112
  • 288
user877329
  • 385
  • 2
  • 13
  • Sharing your research helps everyone. Tell us what you've tried (eg unit tests, code review...) and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see [ask] – gnat Feb 23 '14 at 10:54
  • 1
    ...side note, unless you're designing DTO, _both_ ways suck - because [your objects expose data instead of behavior](http://programmers.stackexchange.com/a/216681/31260) – gnat Feb 23 '14 at 11:02
  • @gnat This is DTO. In this case, there is no way around so I asked what sucks least. – user877329 Feb 23 '14 at 16:32
  • DTOs aren't intended for encapsulation, they're intended to hold data, so the question becomes irrelevant... you should design the DTO for however the thing that will be consuming it needs to see it. – Aaronaught Feb 23 '14 at 18:04
  • @Aaronaught Good point. You may write that as an answer with a little more examples. It is possible for the consumer to pick up the raw pointer from an returned Array reference anyway. – user877329 Feb 23 '14 at 19:42

0 Answers0