[Dprglist] Help with C++ design patterns
deltagraph at aol.com
deltagraph at aol.com
Mon Mar 8 00:01:44 PST 2021
Hey Carl, Just another word. Anytime things are getting a bit verbose with many references to a some nicely named class instance as in your example. I alias it with a shorthand name within the confines of any function that has many references.
I think this is about right looking at your example. If not, I blame it on not having a C++ compiler in my email editor ;)
otherClass *sr = &storedReferenceToOtherClass; // shorthand, typically I define near top of function
sr->function1()
sr->function2() ...
Same thing applies to descriptive variables. If the same variable gets used many times, I am apt to alias it.Of course if it is read only I make a copy of it, if not then pointer to it in C, in Java I just do an assignment at the
end of the method...
int tc = totalNumberOfToadsPresentInCurrrentToadPen;
My rule is short names will be found in local variable declarations with comment OR be an alias of some long name just after the local variable declarations.
Just some ideas...
Ron
-----Original Message-----
From: Carl Ott via DPRGlist <dprglist at lists.dprg.org>
To: jesse brockmann <jjbrockm at gmail.com>; Jian Shi <jshi.fyu at gmail.com>
Cc: DPRG D-List <dprglist at dprg.org>
Sent: Sun, Mar 7, 2021 9:25 am
Subject: Re: [Dprglist] Help with C++ design patterns
Cool thank guys - Sounds like a valid design pattern but with garbled syntax...
On Sun, Mar 7, 2021 at 9:14 AM jesse brockmann <jjbrockm at gmail.com> wrote:
In the class definition use
otherClass *storedReferenceToOtherClass;
And for the calls use
storedReferenceToOtherClass->function ()
On Sun, Mar 7, 2021, 8:32 AM Carl Ott via DPRGlist <dprglist at lists.dprg.org> wrote:
Can somebody help with C++ design patterns?
I have a class with many methods (first class), that needs access to members of another class (other class).
Surely I can pass a reference to the other class into every method of the first class.
But that seems like a verbose pain.
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.
However, I'm missing something - not getting it to work.
Am I just missing syntax or am I fundamentally trying to implement an anti-pattern?
What are some best practices design patterns to accomplish this? Any recommendations - where to find a quick tutorial?
here's an abstract of what isn't working...
class needsToStoreAreference
{
public:
cacheAreference(otherClass &desiredReference);
private:
otherClass &storedReferenceToOtherClass;
}
needsToStoreAreference::cacheAreference(otherClass &desiredReference)
{
storedReferenceToOtherClass= desiredReference; // doesn't work, causes an Arduino program to hang
}
And in the calling code
needsToStoreAreference instanceOfNeedsToStore(); // instance of 'first class' in wording above
otherClass instanceOfOtherClass(); // instance of 'other class' in wording above
instanceOfNeedsToStore.cacheAreference(instanceOfOtherClass);
_______________________________________________
DPRGlist mailing list
DPRGlist at lists.dprg.org
http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org
_______________________________________________
DPRGlist mailing list
DPRGlist at lists.dprg.org
http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dprg.org/pipermail/dprglist-dprg.org/attachments/20210308/2692087b/attachment-0001.html>
More information about the DPRGlist
mailing list