#### Archived

This topic is now archived and is closed to further replies.

# For Every Instance of a Class...

This topic is 5740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey! Is it possible in VC++ to create a class, and make a certain part of the code loop each time for every instance of that class? If not, are class arrays possible? I used a lot of VB and in it, you cannot make an array with classes, so can you do so with C++? Thanks, Voytek

##### Share on other sites
Yes you can create class arrays and they should suffice for something like what you are describing. The efficiency of something scanning for instances of a variable in memory to just having your own class array is almost nil.

A class array would just simply be the following...

class MYCLASS{     int x;     public:         MYCLASS();         ~MYCLASS();}int main(){    MYCLASS carray[10];    // you can use a class array just like any other array, by index    return 0;}

I know only that which I know, but I do not know what I know.

##### Share on other sites
Sounds like what you need are STL vectors. You could use a vector and then use iterators. This basically gives you the same functionality as the VB For Each loop.

##### Share on other sites
Much more functionality than VB

  #include <vector>     //for std::vector#include <iostream>   //for std::cout#include <algorithm>  //for std::for_each#include <functional> //for std::mem_fun_refusing namespace std;class MyClass	{	public:		MyClass()			{			stuff = rand();			}		int stuff;		void Print()			{			cout<<stuff<<endl;			}	};//example usevector<MyClass> my_classes; //make a new vector object containing MyClass''smy_classes.reserve(100); //reserves space for 100 elements, not an essential step, but improves performancesfor(int i=0; i<100; ++i)	{	MyClass data; //MyClass constructor called, stuff set to a random value :)	my_classes.push_back(data); //adds the data instance to the end of the vector (as opposed to the beginning - push_front)	}//This calls the Print method of each MyClass in the vectorfor_each(my_classes.begin(),         my_classes.end(),         mem_fun_ref(MyClass::Print));

It may look a little complicated, but it is very flexible. There''s a more integrated way to do that using an ostream_iterator, but that demonstrates a use of for_each.

##### Share on other sites
i never realized STL was so nifty. what does mem_fun_ref() do?

##### Share on other sites
quote:
Original post by skitch
i never realized STL was so nifty. what does mem_fun_ref() do?

It turns a pointer-to-member function into a function object you can use in an algorithm.

However, I'd like to point you towards the Boost library (see .sig) which provides utilities (boost::function, boost::bind, boost::ref) making the use of algorithms much easier (especially in the more complex cases).

The above call :
for_each(my_classes.begin(),         my_classes.end(),         mem_fun_ref(MyClass::‍Print));

becomes
for_each(my_classes.begin(),         my_classes.end(),         bind(&MyClass::‍Print, _1));

Which means "Bind the first argument (_1) passed by for_each (the object it is iterating over) to the first parameter of &MyClass::‍Print (i.e. the this pointer)".

It looks a bit more complicated that way, but 1) it's a uniform syntax (no more ptr_fun, mem_fun, mem_fun_ref, bind1st...) and 2) it is more flexible (go read the documentation).

Trust me, it will save you a lot of grief.

Too bad VC++ cannot currently compile boost::lambda (look it up too, it saves you on actually _writing_ functors ), though MS 'promised' it for the end of the year...

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

[edited by - Fruny on June 9, 2002 7:58:45 PM]

##### Share on other sites
Thanks for your help! I got it working, finally. :D
-Voytek

[edited by - VoytekG on June 9, 2002 8:41:55 PM]

##### Share on other sites
STL is way bigger than i ever thought
and way more confusing :i