title: Parameter-passing of C++ objects with dynamically allocated memory
body: <p>I'm new to the C++ world, but I have some experience with C and read some tutorials about C++.
  Now, creating objects in C++ seems quite easy and works well for me as long as the class has only attributes that are values (not pointers).</p>
  
  <p>Now, when I try to create objects which allocate memory in the constructor for some of their attributes, I figure out how exactly such objects are passed between functions.
  A simple example of such class would be:</p>
  
  <pre><code>class A {
    int *a;
  public:
    A(int value) {
      this-&gt;a = new int;
      *(this-&gt;a) = value;
    }
    ~A() {
      delete this-&gt;a;
    }
    int getValue() const { return this-&gt;a; }
  }
  </code></pre>
  
  <p>I want to use the class and pass it by value to other functions, etc. At least these examples must work without creating memory leaks or double free errors.</p>
  
  <pre><code>A f1() {
    // some function that returns A
    A value(5);
    // ...
    return value;
  }
  void f2(A a) {
    // takes A as a parameter
    // ...
  }
  
  A a = f1();
  A b = a;
  f2(a);
  f2(f1());
  </code></pre>
  
  <p>The class <code>A</code> is incomplete because I should override <code>operator=</code> and <code>A(A&amp; oldValue)</code> to solve some of these problems.
  As I understand it, the default implementation of these methods just copy the value of the members which is causing the destructor to be called twice on the same pointer values.</p>
  
  <p>Am I right and what else am I missing?
  In addition, do you know any good tutorial that explains this issue?</p>