-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tuesday 19 August 2003 09:16, Jeffrey Clement wrote:
> An array would be the usual way to do this. Makes it way easier to
> programmatically go through them all and do something to them.
>
> C++
> {
> Car cars[30];
> for(int i=0; i<30; i++)
> {
> cars[i] = new Car();
> }
> }
well, personally, in C++ i'd use a vector, a queue, a deque or some other list
template ... easier/safer to work with due to iterators and simplified memory
management. attached is a fun bit of code that demonstrates this... compile
it and try passing various numbers in to it as the first parameter (default
is 10 if no params passed in)
using an array is much more C-ish. in C, if i knew exactly how many of that
data structure i'd need i might use an array ... otherwise i'd make the
struct a linked list...
> PHP is essentially the same.
PHP is a lot less strict when it comes to array management and doesn't really
offer anything much better to use, so using an array here isn't such a bad
idea...
- --
Aaron J. Seigo
GPG Fingerprint: 8B8B 2209 0C6F 7C47 B1EA EE75 D6B7 2EB1 A7F1 DB43
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)
iD8DBQE/QqYr1rcusafx20MRAnvSAKCLC6Wr3YkDb9JASIqA51t6mBiG0QCgg7MR
8sStPb10MlLyWjrd5pN2YoQ=
=CBEm
-----END PGP SIGNATURE-----
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
A()
{
m_id = ++s_count;
}
A(const A&)
{
m_id = ++s_count;
}
int id() const
{
return m_id;
}
static int count()
{
return s_count;
}
private:
static int s_count;
int m_id;
};
int A::s_count = 0;
ostream& operator<< (ostream& stream, const A& a)
{
stream << "I am A" << a.id() << " of " << A::count();
return stream;
}
int main(int argc, char* argv[])
{
int listLength = 10;
if (argc == 2 && argv[1])
{
listLength = atoi(argv[1]);
if (listLength < 1)
{
cerr << "C'mon, you have to specify 1 or more elements!" << endl;
return 0;
}
}
vector<A> aList(listLength);
vector<A>::iterator it = aList.begin();
for (; it != aList.end(); ++it)
{
cout << (*it) << endl;
}
return 0;
}