Newsgroups: comp.lang.c++ Path: utzoo!utgpu!cunews!csi.uottawa.ca!news From: hitz@sim5.csi.uottawa.ca (Martin Hitz) Subject: Re: const vs. static in class declaration Message-ID: <1991Apr3.202015.7402@csi.uottawa.ca> Keywords: const, static, class constants Sender: news@csi.uottawa.ca Nntp-Posting-Host: sim5 Organization: University of Ottawa References: <286@dayton.stanford.edu> <139@grenada.island.COM> <3864@island.COM> Distribution: na Date: Wed, 3 Apr 91 20:20:15 GMT In article <3864@island.COM> chris@island.COM (Chris King) writes: >Initialization of const static class members are one thing that I really >think have been poorly thought out in c++. > [...] >rather than >use a define (I hate defines), I usually do the following: > >class array_of_jive { > struct jive a[10]; > > inline int num_elements() { return(sizeof(a)/sizeof(struct jive)); } > > void tweak_all() > { > for ( int i = 0 ; i < num_elements(); ++i ) > a[i].tweak(); > } >}; > >Has anybody found a easier way of doing this. No. I agree that this is a major uglyness in C++. However, I *do* use a define in such a case to avoid declaration of N inline functions for N arrays: #define DIM(array) (sizeof(array)/sizeof(*array)) class array_of_jive { struct jive a[10]; void tweak_all() { for ( int i = 0 ; i < DIM(a); ++i ) a[i].tweak(); } }; Martin Hitz (hitz@csi.UOttawa.CA)