<div dir="ltr">Hi Carl,<div><br></div><div>Can you save the reference in a pointer? i.e. "otherClass* storedReferenceToOtherClass"</div><div>The pattern seems ok.</div><div><br></div><div>I think you need to initialize the reference in the constructor, otherwise the field "otherClass &storedReferenceToOtherClass" doesn't have a valid state.</div><div><br></div><div>Regards,</div><div>-Jian</div><div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><p style="margin:0in 0in 12pt;font-size:11pt;font-family:Calibri,sans-serif;text-align:justify"><b><span style="font-size:8pt;font-family:Arial,sans-serif;color:black">Jian Shi</span></b><span style="font-size:8pt;font-family:Arial,sans-serif;color:black"><u></u><u></u></span></p><p style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif;text-align:justify"><span style="font-size:8pt;font-family:Arial,sans-serif;color:black">Phone: <a href="tel:%2B1%20805-680-6412" value="+18056806412" style="color:rgb(17,85,204)" target="_blank">+1 805-680-6412</a><u></u><u></u></span></p><p style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><span style="font-size:8pt;font-family:Arial,sans-serif;color:black">Email: </span><u><span style="font-size:8pt;font-family:Arial,sans-serif;color:blue"><a href="mailto:jshi.fyu@gmail.com" style="color:rgb(17,85,204)" target="_blank"><span style="color:blue">jshi.fyu@gmail.com</span></a></span></u></p></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Mar 7, 2021 at 8:32 AM Carl Ott via DPRGlist <<a href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><div>Can somebody help with C++ design patterns?<br><br>I have a class with many methods (first class), that needs access to members of another class (other class).<br>Surely I can pass a reference to the other class into every method of the first class.<br>But that seems like a verbose pain.<br>Instead, I'd like to initialize the first class with a reference or pointer to the other class. Then the first class could use the cached reference or pointer to the other class whenever it's needed.<br>However, I'm missing something - not getting it to work.</div><div><br>Am I just missing syntax or am I fundamentally trying to implement an anti-pattern?<br><br>What are some best practices design patterns to accomplish this?  Any recommendations - where to find a quick tutorial?<br><br><br>here's an abstract of what isn't working...<br><br>class needsToStoreAreference  <br>{<br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>public:</div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>       cacheAreference(otherClass &desiredReference);</div></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>private:</div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>   otherClass &storedReferenceToOtherClass;</div></blockquote></blockquote><div>}<br><br>needsToStoreAreference::cacheAreference(otherClass &desiredReference)<br>{<br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div> 

storedReferenceToOtherClass

= desiredReference;     </div><div> // doesn't work, causes an Arduino program to hang</div></blockquote><div>}<br><br><br><br>And in the calling code<br><br>needsToStoreAreference instanceOfNeedsToStore();   // instance of 'first class' in wording above<br><br>otherClass instanceOfOtherClass();             // instance of 'other class' in wording above<br><br>instanceOfNeedsToStore.cacheAreference(instanceOfOtherClass);<br><br></div></div>
_______________________________________________<br>
DPRGlist mailing list<br>
<a href="mailto:DPRGlist@lists.dprg.org" target="_blank">DPRGlist@lists.dprg.org</a><br>
<a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" rel="noreferrer" target="_blank">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br>
</blockquote></div>