<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>I wouldn't call this a patter, just a technique.<br>
<br>
A reference member must be initialized in the constructors
initialization list. It cannot be done through assignment. I'm
surprised the compiler didn't generate a syntax error with your
code. But then the Arduino system does weird stuff with the code
which satisfies the compiler but is not correct at run time. Have
to see an actual simple example to tell. <br>
</p>
<p>The other possibility is you haven't created the class. In the
code below A must be initialized before it can be passed to B;<br>
</p>
<p><font face="monospace">class A {</font></p>
<p><font face="monospace">};</font></p>
<p><font face="monospace">class B {</font></p>
<p><font face="monospace">public:</font></p>
<p><font face="monospace"> B(A& a) : // by refernce<br>
mA{a} {}<br>
</font></p>
<p><font face="monospace"> B(A* a); // by pointer<br>
mAPtr{a};</font></p>
<p><font face="monospace">private:</font></p>
<p><font face="monospace"> A& mA;<br>
A* mAPtr;<br>
}</font></p>
<p><font face="monospace">A a;<br>
B b{a}; // by ref<br>
B b_ptr{&a}; // by ptr<br>
</font></p>
<p><font face="monospace"><br>
</font></p>
<p><br>
</p>
<div class="moz-signature"><font face="Comic Sans MS"
color="#000080"> <br>
-73 - <br>
<b>Rud Merriam K5RUD</b> <br>
<a href="http://mysticlakesoftware.com/"> <i>Mystic Lake
Software</i> </a> <br>
<br>
</font>
</div>
<div class="moz-cite-prefix">On 3/7/21 8:32 AM, Carl Ott via
DPRGlist wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CA+XqQZTzOXgFtHVymSqUehtzWRwsf7JddHTPzmk7umNskBrXAA@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<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:0 0 0 40px;border:none;padding:0px">
<div>public:</div>
</blockquote>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div> cacheAreference(otherClass &desiredReference);</div>
</blockquote>
</blockquote>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div>private:</div>
</blockquote>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div> otherClass &storedReferenceToOtherClass;</div>
</blockquote>
</blockquote>
<div>}<br>
<br>
needsToStoreAreference::cacheAreference(otherClass
&desiredReference)<br>
{<br>
</div>
<blockquote style="margin:0 0 0 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>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
DPRGlist mailing list
<a class="moz-txt-link-abbreviated" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a>
<a class="moz-txt-link-freetext" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a>
</pre>
</blockquote>
</body>
</html>