"James" <ja***********@hotmail.com> wrote in message
news:b7**************************@posting.google.c om...
Hi, I'm hoping someone can help me out.
If I declare a class, eg.
class CSomeclass
{
public:
var/func etc.....
private
varfunc etc..
};
Then in the code I create a pointer to this class definition
CSomeClass *MyClass;
I want to be able to dynamically increase the size of the
class as I go. Imagine I have a current count of the
object - eg. Count. Whenever I try to allocate
memory for it,
Count++;
MyClass[Count] = new SomeClass;
MyClass is a pointer to CSomeClass, so MyClass[Count] is a CSomeClass, but
new CSomeClass is a pointer. So you are assigning a CSomeClass* to a
CSomeClass, not surprisingly the compiler complains.
I think you don't have a good understanding of pointers and arrays, so I'll
recommend the easy way (I'd actually recommend this in any case). Here's a
very brief tutorial in vectors
#include <vector>
std::vector<CSomeClass> MyClass; // a zero sized vector
cout << MyClass.size() << '\n'; // this prints zero
MyClass.push_back(CSomeClass()); // add an object to the vector
cout << MyClass.size() << '\n'; // now this prints one
MyClass.push_back(CSomeClass()); // add another object to the vector
cout << MyClass.size() << '\n'; // now this prints two
for (int i = 0; i < MyClass.size(); ++i) // loop though all the objects in
the vector
{
CSomeClass obj = MyClass[i]; // get an object out the vector
// do something with it
}
Get the picture? Learn about vectors, they will make your life so much
easier.
If you insist on doing it with pointers, then realise that the only way to
do it is to allocate a whole new array, bigger than the old array and the
copy all the elements one by one from the old array to the new array and
then delete the old array.
john