[Development] Single static list object to track something

Marc Mutz marc.mutz at kdab.com
Fri Sep 21 07:26:30 CEST 2012


On Friday September 21 2012, Majid Khan wrote:
> Hi,
>
> I have a class that tracks strings so essentially TrackedStrings :
> QStringList and this contains a list of strings that are being tracked (so
> some function from some other class calls it's add(const QString& string).
> I need this object as static because I want to access it from anywhere
> without having to define it.
>
> Now everytime TrackedStrings.h header is used, it initializes it even
> though I have used prevent guard (I checked it by having a break point on
> constructer).
>
> What is the best way to use this static TrackedStrings trackedStrings;
> object in order to use it from anywhere. Remember, I don't want to
> explicitly initialize it (on heap or stack).
>
> Thanks

http://en.wikipedia.org/wiki/Singleton_pattern

Your TrackedStrings won't is-a QStringList, so don't inherit from QStringList, 
aggregate instead: http://en.wikipedia.org/wiki/Liskov_substitution_principle

-- 
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions



More information about the Development mailing list