Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As answered by others (James McNellis' <a href="https://stackoverflow.com/questions/3621551/c-advice-on-writing-code/3621593#3621593">answer</a> comes to mind), you should switch to <code>std:string</code> instead of <code>char *</code>.</p> <p>Your problem is that repetition can't be avoided (both non default constructor and the setAddress method set the data), and having one calling the other could be less effective.</p> <p>Now, the real problem, I guess, is that your code is doing a lot, which means that repetition of delicate code could be dangerous and buggy, thus your need to have one function call the other. This need can be remove by using the <code>std::string</code>, as it will remove the delicate code from your code altogether.</p> <p>As it was not shown Let's re-imagine your class:</p> <pre><code>class Address { public : Address() ; Address(const std::string &amp; p_city , const std::string &amp; p_street , int p_buildingNumber , int p_apartmentNumber) ; // Etc. private : std::string m_city ; std::string m_street ; int m_buildingNumber ; int m_apartmentNumber ; } ; </code></pre> <p>Using the <code>std::string</code> instead of the <code>const char *</code> will make the <code>std::string</code> object responsible for handling the resource (the string itself).</p> <p>For example, you'll see I wrote no destructor in the class above. This is not an error, as without a destructor, the compiler will generate its own default one, which will handle the destructor of each member variable as needed. The <code>remove</code> you use for resource disposal (freeing the unused <code>char *</code>) is useless, too, so it won't be written. This means a lot of delicate code that won't be written, and thus, won't produce bugs.</p> <p>And it simplifies greatly the implementation of the constructors, or even the setAddress method :</p> <pre><code>Address::Address() // std::string are initialized by default to an empty string "" // so no need to mention them in the initializer list : m_buildingNumber(0) , m_apartmentNumber(0) { } Address::Address(const std::string &amp; p_city , const std::string &amp; p_street , int p_buildingNumber , int p_apartmentNumber) : m_city(p_city) , m_street(p_street) , m_buildingNumber(p_buildingNumber) , m_apartmentNumber(p_apartmentNumber) { } void Address::setAddress(const std::string &amp; p_city , const std::string &amp; p_street , int p_buildingNumber , int p_apartmentNumber) { m_city = p_city ; m_street = p_street ; m_buildingNumber = p_buildingNumber ; m_apartmentNumber = p_apartmentNumber ; } </code></pre> <p>Still, there is repetition in this code, and indeed, we'll have to wait C++0x to have less repetition. But at least, the repetition is trivial, and easy to follow: No dangerous and delicate code, everything is simple to write and read. Which makes your code more robust than the <code>char *</code> version.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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