Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong> Answering the edited question :)</p> <blockquote> <p><strong>this is simple contrived example to</strong> <strong>discuss using getters/setters vs</strong> <strong>variables</strong></p> </blockquote> <p>If you have a simple collection of variables, that don't need any kind of validation, nor additional processing then you might consider using a POD instead. From <a href="http://www.research.att.com/~bs/bs_faq.html#class" rel="nofollow noreferrer">Stroustrup's FAQ</a>:</p> <blockquote> <p>A well-designed class presents a clean and simple interface to its users, <strong>hiding its representation</strong> and saving its users from having to know about that representation. If the representation shouldn't be hidden - say, because users should be able to change any data member any way they like - you can think of that class as "just a plain old data structure"</p> </blockquote> <p>In short, this is not JAVA. you shouldn't write plain getters/setters because they are as bad as exposing the variables them selves.</p> <blockquote> <p><strong>initializer lists or private validator functions are not really part of this question. I'm wondering if either design will make the code easier to maintain and expand.</strong></p> </blockquote> <p>If you are copying another object's variables, then the source object should be in a valid state. How did the ill formed source object got constructed in the first place?! Shouldn't constructors do the job of validation? aren't the modifying member functions responsible of maintaining the class invariant by validating input? Why would you validate a "valid" object in a copy constructor?</p> <blockquote> <p><strong>I'm not concerned about performance. we're not programming on the PDP-11</strong></p> </blockquote> <p>This is about the most elegant style, though in C++ the most elegant code has the best performance characteristics usually.</p> <hr> <p>You should use an <code>initializer list</code>. In your code, <code>m_str1</code> is default constructed <strong>then</strong> assigned a new value. Your code could be something like this:</p> <pre><code>class Container { public: Container() {} Container(const Container&amp; cont) : m_str1(cont.m_str1) { } string GetMyString() { return m_str1;} void SetMyString(string str) { m_str1 = str;} private: string m_str1; }; </code></pre> <hr> <p>@cbrulak You shouldn't IMO validate <code>cont.m_str1</code> in the <code>copy constructor</code>. What I do, is to validate things in <code>constructors</code>. Validation in <code>copy constructor</code> means you you are copying an ill formed object in the first place, for example:</p> <pre><code>Container(const string&amp; str) : m_str1(str) { if(!valid(m_str1)) // valid() is a function to check your input { // throw an exception! } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload