Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two things wrong with your code, that need to be fixed before answering the question.</p> <ol> <li>The fact that those global pointers are global - having something global, at global namespace, is sign of bad design (if your teacher is advocating it - because it seems kinda like homework - then slap him in the face for doing so).</li> <li>The fact that those global pointers are raw pointers - you either want to keep the ownership in their place and not pass the ownership around - then you'll use either <code>unique_ptr</code> or just plain object (unless the object is supposed to live longer than <code>main()</code>, but that's a weird case).</li> </ol> <p>So, after correcting the code, it looks like this:</p> <pre><code>int main() { A a; A b(a); } </code></pre> <p>If you need to access those objects from other parts of the code, without explicitly passing them around, put them in sensibly named <code>namespace</code>:</p> <pre><code>// in header namespace a_and_b // this is *wrong* name for it, of course { extern A a; extern A b; } // in one of TUs - also possible to wrap this in namespace ... { ... } A a_and_b::a; A a_and_b::b(a_and_b::a); </code></pre> <p>Of course, if you are just asking for syntax, the answer would be:</p> <pre><code>A * b = new A(*a); </code></pre> <p>so just dereference the pointer to get <code>A</code> out of <code>A *</code>. But please, don't ever do this - even in freestanding environment you can easily implement own smart pointer to wrap this in sane way.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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