Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, to address your immediate question, if I understand correctly you want to know if you can (1) create a shared pointer with local scope, (2) push it on to the back of a reference to a vector of shared pointers, and (3) have the pointer refer to the same object as when you created it. The answer to all of these is yes, and you get that behavior pretty much for free. For example:</p> <pre><code>void push_shared_pointer(std::vector&lt;boost::shared_ptr&lt;int&gt; &gt; &amp;vector) { boost::shared_ptr&lt;int&gt; int_ptr(new int(5)); vector.push_back(int_ptr); *int_ptr = 6; } std::vector&lt;boost::shared_ptr&lt;int&gt; &gt; int_vector; push_shared_pointer(int_vector); std::cout &lt;&lt; *int_vector.back( ) &lt;&lt; std::endl; </code></pre> <p>This outputs 6 because in the function since after we pushed the shared pointer onto the vector, we used the local shared pointer to modify the referenced int. Since the shared pointer in the vector and the local shared pointer refer to the same memory location, both shared pointers are updated.</p> <p>As for the rest of the code, there a few things to note. First, the preprocessor guards were an issue, as others noted. Second, you didn't scope the function blah() in SopttingMarker.cpp to tell the compile that it's an implementation of the blah function in the SpottingMarker class. It should read:</p> <pre><code>#include "SpottingMarker.h" void SpottingMarker::blah() { } </code></pre> <p>In addition, since you indicated in the header that you are going to provide a user defined default constructor, you should provide that in SpottingMarker.cpp as well.</p> <pre><code>SpottingMarker::SpottingMarker() : mPosition(0) , mDuration(0) , mDescription(NULL) { } </code></pre> <p>Third, since you declared a destructor in CsvSpottingNotes.h, you should define it in CsvSpottingNotes.cpp:</p> <pre><code>CsvSpottingNotes::~CsvSpottingNotes() { // implementation here } </code></pre> <p>Those should be all the definitions you need, at least according to your build log.</p> <p>The last thing I would note is that it doesn't look like your using dependency injection in a traditional manner for three reasons. First, typically when you do dependency injection you store a reference to the injected object in the receiver object so the receiver can use over the course of its lifetime. Here you inject the vector into CsvSpottingNotes, but you don't store a reference to it in the class; you just push a shared pointer onto it.</p> <p>Second, in my experience dependency injection is for behavioral abstractions, i.e. to separate the behavior of the receiver (client) object from the behavior of the injected object using an interface. This allows you to modify the behavior of the client by injecting different implementations of the interface. In this case you aren't using an interface; std::vector is a fully qualified type at compile time and CsvSpaottingMarker can't be used with any other type, so there's no reason to use dependency injection. A better solution is to store the vector as a member of CsvSpottingMarker.</p> <p>Third, as I said before, dependency injection is used mainly for behavioral abstractions, i.e. getting client objects to behave differently depending on the implementation of the interfaces that are injected into them. The std::vector class is not a good candidate for dependency injection because it does not provide a behavior; instead, it merely saves state. Generally the state of the object should be encapsulated as a member of the object class, which is further support for including the vector in the CsvSpottingMarker class.</p> <p>Hope that helps!</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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