Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no hard and fast rule:<br> As people have mentioned using references inside objects can cause copy problems (and it does) so it is not a panacea, but for certain situation it can be useful (that is why C++ gives us the option to do it all these different ways). But using RAW pointers is really not an option. If you are dynamically allocating objects then you should always be maintaining them with smart pointers and your object should also be using smart pointers.</p> <p>For people who demand examples: Streams are always passed and stored as references (as they can't be copied).</p> <p>Some Comments on your code examples:</p> <h3>Example one and two</h3> <blockquote> <p>Your first example with pointers. Is basically the same as the second example using references. The difference being that a reference can not be NULL. When you pass a reference the object is already alive and thus should have a lifespan greater than the object you are testing already (If it was created on the stack) so it should be safe to keep a reference. If you are dynamically creating pointers as dependencies I would consider using boost::shared_pointer or std::auto_ptr depending if ownership of the dependency is shared or not.</p> </blockquote> <h3>Example Three:</h3> <blockquote> <p>I don't see any great use for your third example. This is because you can not use polymorphic types (If you pass an object derived from Dependency it will be sliced during the copy operation). Thus the code may as well be inside Addict rather than a separate class.</p> </blockquote> <h3>Bill Harlen: (<a href="http://billharlan.com/pub/papers/Managing%5FCpp%5FObjects.html" rel="noreferrer">http://billharlan.com/pub/papers/Managing%5FCpp%5FObjects.html</a>)</h3> <p>Not to take anything away from Bill But:</p> <ol> <li>I have never heard of him. <ul> <li>He is a Geo-Physists not a computer programmer</li> <li>He recomends programming in Java to improve your C++</li> <li>The languages are now so different in usage that is utterly false).</li> <li>If you want to use references of What to-do/not to-do.<br> Then I would pick one of the Big names in the C++ field:<br> Stroustrup/Sutter/Alexandrescu/Meyers</li> </ul></li> </ol> <h3>Summary:</h3> <ol> <li>Don't use RAW pointers (when ownership is required)</li> <li>Do use smart pointers.</li> <li>Don't copy objects into your object (it will slice).</li> <li>You can use references (but know the limitations).</li> </ol> <h3>My example of Dependency injection using references:</h3> <pre><code>class Lexer { public: Lexer(std::istream&amp; input,std::ostream&amp; errors); ... STUFF private: std::istream&amp; m_input; std::ostream&amp; m_errors; }; class Parser { public: Parser(Lexer&amp; lexer); ..... STUFF private: Lexer&amp; m_lexer; }; int main() { CLexer lexer(std::cin,std::cout); // CLexer derived from Lexer CParser parser(lexer); // CParser derived from Parser parser.parse(); } // In test.cpp int main() { std::stringstream testData("XXXXXX"); std::stringstream output; XLexer lexer(testData,output); XParser parser(lexer); parser.parse(); } </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. 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.
 

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