[Insight-developers] Caution about static class members and OS X

Williams, Norman K norman-k-williams at uiowa.edu
Thu Mar 24 12:47:15 EDT 2011


I was squawking earlier this week about seeing a static member variable of
itk::ObjectFactoryBase having more than one instance when accessed by
derived classes in different libraries.  This led to me moving the static
member out of the class and into a static variable in a patch pushed the
other day.

http://review.source.kitware.com/#change,1244

Well, it seemed to me that this wasn't the way static member variables
were supposed to behave in C++, so I tested this hypothesis by backing out
that patch on a linux box, recompiling and re-running the tests.

On Linux (RHEL6 to be exact) I couldn't duplicate the problem, so I think
it's safe to blame Apple for this one.

Be that as it may, static member variables are problematic for a number of
reasons and should probably be avoided.  There might be a good reason to
use them, but given the OS X weirdness about them, it seems better to use
static variables outside the class instead.

This should only come up in the context of the singleton pattern, and in
ITK, the only singleton I know of is the Object Factory.
--
Kent Williams norman-k-williams at uiowa.edu






________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited.  Please reply to the sender that you have received the message in error, then delete it.  Thank you.
________________________________


More information about the Insight-developers mailing list