Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A copy constructor is a constructor with a specific signature. It takes a single parameter, of const reference to the same type being constructed. So for a <code>class Foo</code>, the copy constructor looks like this:</p> <pre><code>Foo::Foo(const Foo&amp;) </code></pre> <p>There are 2 other kinds of constructors: default and convert. The default constructor takes no parameters (or can be called as if it took no parameters, eg, all parameters have defaults):</p> <pre><code>Foo::Foo() </code></pre> <p>...and the convert destructor is basically anything else. For example:</p> <pre><code>Foo::Foo(const Bar&amp;) </code></pre> <hr> <blockquote> <p>why is the copy constructor being called?</p> </blockquote> <p>In your case, you're calling a <code>RailwayLineLink</code> constructor but passing in a const reference to a <code>RailwayLine</code>, so you're actually calling a convert constructor, not a copy constructor.</p> <p>The convert constructor is called because you called it:</p> <pre><code>new RailwayLineLink(data); </code></pre> <p>This constructs a new <code>RailwayLineLink</code> object, passing <code>data</code> as the one and only parameter to the constructor.</p> <p>If you have an empty convert constructor, that looks something like this:</p> <pre><code>RailwayLineLink::RailwayLineLink(const RailwayLine&amp;) { } </code></pre> <p>...then all of the members of the new copy will be default constructed, and probably contain garbage.</p> <p>Typically when you implement a copy constructor, you would want to do some kind of memberwise copy from the original to the copy, like this:</p> <pre><code>RailwayLineLink::RailwayLineLink(const RailwayLine&amp; rhs) : value_(rhs.value_) { } </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