Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct syntax to construct a class with a shared_ptr as member field?
    primarykey
    data
    text
    <p>Suppose, I want to make a class <code>B</code> with a pointer to another class <code>A</code>. In the constructor of <code>A</code> I want to construct <code>B</code> and passing it a pointer to <code>this</code>. In principle, this pointer can be a <code>shared_ptr</code>, correct? Then how do I make such a class with the assignment of the <code>shared_ptr</code> in the constructor?</p> <p>I tried it in the code below. <code>ClassB</code> and <code>ClassC</code> are identical, with a pointer to <code>ClassA</code> which constructs instances of <code>ClassB</code> and <code>ClassC</code>. The only two differences are that <code>ClassB</code> holds a regular pointer and <code>ClassC</code> holds a <code>shared_ptr</code> to <code>ClassA</code> and that the code with <code>ClassB</code> is working and the code with <code>ClassC</code> is not. What am I doing wrong?</p> <pre><code>// Main.cpp : Defines the entry point for the console application. #include "ClassA.h" #include &lt;iostream&gt; int main(int argc, char* argv[]) { std::cout &lt;&lt; "Create B and C separately, without a reference to A:" &lt;&lt; std::endl; ClassB(nullptr); ClassC(nullptr); std::cout &lt;&lt; "Create A, and through it B and C:" &lt;&lt; std::endl; ClassA A; A.PrintHello(); A.getObjectB().getPointerToA()-&gt;PrintHello(); A.PrintHello(); A.getObjectC().getPointerToA()-&gt;PrintHello(); return 0; } // ClassA.h #pragma once #include "ClassB.h" #include "ClassC.h" class ClassA { private: ClassB objectB; ClassC objectC; public: ClassA(void); ClassB getObjectB() { return objectB; }; ClassC getObjectC() { return objectC; }; void PrintHello(); }; // ClassA.cpp #include "ClassA.h" #include &lt;iostream&gt; #include &lt;memory&gt; ClassA::ClassA(void) : objectB(ClassB( this )), objectC(ClassC( std::make_shared&lt;ClassA&gt;(*this) )) { std::cout &lt;&lt; "Class A fully constructed" &lt;&lt; std::endl; } void ClassA::PrintHello() { std::cout &lt;&lt; "Hello" &lt;&lt; std::endl; } // ClassB.h #pragma once #include &lt;memory&gt; class ClassA; class ClassB { private: ClassA* pointerToA; public: ClassB(ClassA* pA); ClassA* getPointerToA() { return pointerToA; }; }; // ClassB.cpp #include "ClassB.h" #include &lt;iostream&gt; ClassB::ClassB(ClassA* pA) : pointerToA(pA) { std::cout &lt;&lt; "Class B constructed" &lt;&lt; std::endl; } // ClassC.h #pragma once #include &lt;memory&gt; class ClassA; class ClassC { private: std::shared_ptr&lt;ClassA&gt; pointerToA; public: ClassC(std::shared_ptr&lt;ClassA&gt; pA); std::shared_ptr&lt;ClassA&gt; getPointerToA() { return pointerToA; }; }; // ClassC.cpp #include "ClassC.h" #include &lt;iostream&gt; ClassC::ClassC(std::shared_ptr&lt;ClassA&gt; pA) : pointerToA(pA) { std::cout &lt;&lt; "Class C constructed" &lt;&lt; std::endl; } </code></pre>
    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.
 

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