|
Prev: Large integers in C++
Next: Big Bertha Thing letter
From: Vijaya Kumar B.H. on 2 Apr 2008 09:46 I am working on a project where i have to do same processing for all the members of the structure. The problem is the struct is very huge (with about 20 ints) and i have to write the specific function to each structure member. I am a java programmer also , java has a concept called reflection which helps to extract the members from the object. I am just wondering if such a way is available in C++. I saw few external libraries which can do such thing but they involve log of changes in structure. any better idea ? Vijay
From: Bart van Ingen Schenau on 2 Apr 2008 12:56 Vijaya Kumar B.H. wrote: > I am working on a project where i have to do same processing for all > the members of the structure. The problem is the struct is very huge > (with about 20 ints) and i have to write the specific function to each > structure member. > > I am a java programmer also , java has a concept called reflection > which helps to extract the members from the object. I am just > wondering if such a way is available in C++. I saw few external > libraries which can do such thing but they involve log of changes in > structure. > > any better idea ? If it occurs frequently that you need to apply the same operation to all (or a large portion) of the data members, you could choose to declare them as a single array (provided they all have the same type). When you need to access a particular member, you can use an enumeration to give the members a name. For example: class Foo { private: enum { first_member, boo, bar, num_members /* must be last! */ }; int data[num_members]; public: void increment_all() { for (int i=0; i<num_members; i++) data[i]++; } int get_boo() { return data[boo]; } }; > > Vijay Bart v Ingen Schenau -- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://c-faq.com/ c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
From: Francis Glassborow on 3 Apr 2008 07:25 Bart van Ingen Schenau wrote: > Vijaya Kumar B.H. wrote: > >> I am working on a project where i have to do same processing for all >> the members of the structure. The problem is the struct is very huge >> (with about 20 ints) and i have to write the specific function to each >> structure member. >> >> I am a java programmer also , java has a concept called reflection >> which helps to extract the members from the object. I am just >> wondering if such a way is available in C++. I saw few external >> libraries which can do such thing but they involve log of changes in >> structure. >> >> any better idea ? > > If it occurs frequently that you need to apply the same operation to all > (or a large portion) of the data members, you could choose to declare > them as a single array (provided they all have the same type). When you > need to access a particular member, you can use an enumeration to give > the members a name. > > For example: > > class Foo > { > private: > enum { > first_member, > boo, > bar, > num_members /* must be last! */ > }; > int data[num_members]; > public: > void increment_all() > { > for (int i=0; i<num_members; i++) > data[i]++; > } > int get_boo() > { > return data[boo]; > } > }; > Or youy can create an array and then declare references to individual elements to give them names: class Foo { public: // whatever private: static int const datacount = 20; int data[datacount]; int & first(data[0]); int & second(data[1]); // etc. but with more meaningful names }; Now you can access the data as an array when you want to process all the members the same way or by name when you want the individual elements.
From: Vijaya Kumar B.H. on 3 Apr 2008 09:02 Thanks Francis , Schenau I will try these methods Vijay "Francis Glassborow" <francis.glassborow(a)btinternet.com> wrote in message news:r6qdnVPRSOc5I2nanZ2dnUVZ8v6dnZ2d(a)bt.com... > Bart van Ingen Schenau wrote: >> Vijaya Kumar B.H. wrote: >> >>> I am working on a project where i have to do same processing for all >>> the members of the structure. The problem is the struct is very huge >>> (with about 20 ints) and i have to write the specific function to each >>> structure member. >>> >>> I am a java programmer also , java has a concept called reflection >>> which helps to extract the members from the object. I am just >>> wondering if such a way is available in C++. I saw few external >>> libraries which can do such thing but they involve log of changes in >>> structure. >>> >>> any better idea ? >> >> If it occurs frequently that you need to apply the same operation to all >> (or a large portion) of the data members, you could choose to declare >> them as a single array (provided they all have the same type). When you >> need to access a particular member, you can use an enumeration to give >> the members a name. >> >> For example: >> >> class Foo >> { >> private: >> enum { >> first_member, >> boo, >> bar, >> num_members /* must be last! */ >> }; >> int data[num_members]; >> public: >> void increment_all() >> { >> for (int i=0; i<num_members; i++) >> data[i]++; >> } >> int get_boo() >> { >> return data[boo]; >> } >> }; >> > Or youy can create an array and then declare references to individual > elements to give them names: > > class Foo { > public: > // whatever > private: > static int const datacount = 20; > int data[datacount]; > int & first(data[0]); > int & second(data[1]); > // etc. but with more meaningful names > }; > > Now you can access the data as an array when you want to process all the > members the same way or by name when you want the individual elements.
|
Pages: 1 Prev: Large integers in C++ Next: Big Bertha Thing letter |