   Arrays of Classes
  Hi i want to allocate memory for an array of pointer to a class, with an int i would do :int *i = new int[iMyValue];i[0] = 10;i[1] = 5; etc etcHowever this technique does not seem to work with my classes :class MyClass;MyClass *hello = new MyClass[iMyValue];hello[0] = new MyClass;hello[1] = new MyClass; etc etc So I want a dynamic array of pointer to MyClass. Whats the best way to do this?Cheers 
  i would recommend using the STL, and do something like this:std::vector vecMyClasses;for(long n = 0; n < MAX_CLASSES; ++n){   MyClass* pcMyClass = new MyClass();   vecMyClasses.push_back(pcMyClass);}// then you canfor(long n = 0; n < MAX_CLASSES; ++n){   vecMyClasses[n].MyFunction();}// or better yet,for(std::vector::const_iterator itrMyClass = vecMyClasses.begin(); itrMyClass!= vecMyClasses.end(); ++itrMyClasses){   itrMyClass->MyFunction();}// and later, when you are finished, a littlestd::vector::iterator itrMyClass = vecMyClasses.begin();while(itrMyClass != vecMyClasses.end()){   MyClass* pcMyClass = *itrMyClass;   itrMyClass = vecMyClasses.erase(itrMyClass);   delete pcMyClass;}/* SUPER DISCLAIMER - i wrote this code here, not on my compiler, so no gaurantees that its bug-free */i find the stl to be really nice - once you understand how it works, it saves *huge* time! just a quick note - i usually use boost's smart pointer rather than the raw pointer given in the example above. it's a little safer, but a more keystrokes...---craig(at)cod3rz(dot)net   [edited by - craigatcod3rzdotnet on February 16, 2004 12:30:51 PM];quote:Original post by jonbell  MyClass *hello = new MyClass[iMyValue];hello[0] = new MyClass;hello[1] = new MyClass; snip snip  So I want a dynamic array of pointer to MyClass. Whats the best way to do this?As craigatcod3rzdotnet said, your best bet is to use the STL: std::vector<MyClass*>, and even use boost''s smart_ptr''s.The problem with the line hello[0] = new MyClass;   is that "hello[0]" is already a class, but the right hand side "new MyClass" is returning a pointer.  So basically you''re creating a memory leak that doesn''t compile. ;MyClass** hello = new MyClass[some_number];hello[0] = new MyClass;hello[1] = new MyClass;etc...... "A soldier is a part of the 1% of the population that keeps the other 99% free"  - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA ;  The last one was close, except declaring the array should be like so:MyClass** hello = new MyClass*[some_number];  That extra asterisk between MyClass and [some_number] is important.  And don''t forget to delete every single element, and then the entire array when you''re done.  It''d be even better if you could just use an STL container of MyClass*, like others have mentioned, or if you don''t have to dynamically create them, and can use the default constructor, then a container of MyClass objects, not pointers, would be the best.  That way, you wouldn''t have to worry about newing and deleting memory at all. ;  Just to be pedantic, in C++ you can''t have arrays of classes. You can, however, have arrays of objects.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  "Just to be pedantic, in C++ you can't have arrays of classes. You can, however, have arrays of objects. "Do you have much else to do?Oh and I don't want to use STL because at some point I may want to go cross platform and STL has problems (particularly on PS2).   [edited by - jonbell on February 17, 2004 4:17:00 AM];  This is all you need to do,MyClass *hello = new MyClass[iMyValue];hello now points to an array of MyClass objects. You need not new them again.To access them you just do so like a normal array. hello[i].sayHello();  [edited by - GamerSg on February 17, 2004 6:11:57 AM];  Do not forget that the method described by GamerSg wont call class constructors. ;quote:Original post by _Rambo_  Do not forget that the method described by GamerSg wont call class constructors.   Not true.The default constructor will be called for every element in the array.Marco.