Array of classes

J

James

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;

But it throws an error:
error C2679: binary '=' : no operator found which takes
a right-hand operand of type 'CAlien *'
(or there is no acceptable conversion)

I have no idea how many instances I am going to need ahead of time,
and I can't find and combination of the line that allows it to work.
If anyone has any advicce on what I'm doing wrong it would be much
apreciated. Thank you.

James Alger
 
L

Leor Zolman

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;

First of all, let's make sure you know what you've got here. The way you've
defined it, MyClass is a pointer to a CSomeClass objet (or an array of
them), but the objects don't yet exist. In order for it to actually point
to an array of objects of some given size n, you'd have to set things up as
follows:

MyClass = new CSomeClass[n];

The size 'n' is fixed at that point, and all of the objects get default
initialized.
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;

(Did you mean CSomeClass rather than SomeClass?)

Two things wrong here. First, your "Count" variable can be used to keep
track of how many items in an existing array you've actually used, but that
doesn't affect the size of the array. Second, your assignment statement is
treating elements of the MyClass dynamic array as if they were each a
pointer. This may in fact be what you want, but then the definition of
MyClass would have had to be:

CSomeClass **MyClass;

and you'd have had to initialize it by creating a dynamic array of
/pointers/ to CSomeClass, e.g.:

CSomeClass = new CSomeClass *[n];

Then you can dynamically create new CSomeClass objects (using 'new' as you
did) and gradually fill in the array of pointers. If you know the upper
maximum for the number you need, and it isn't outrageously high, then
choosing 'n' above to be large enough would give you a fairly efficient
implementation. The elements you never used wouldn't cost you much (just a
pointer's worth of memory in the dynamic array).
But it throws an error:
error C2679: binary '=' : no operator found which takes
a right-hand operand of type 'CAlien *'
(or there is no acceptable conversion)

I have no idea how many instances I am going to need ahead of time,
and I can't find and combination of the line that allows it to work.
If anyone has any advicce on what I'm doing wrong it would be much
apreciated. Thank you.

If you truly don't know how many instances you'll need, you probably just
want to use a std::vector to hold CSomeClass objects by value. If the
objects are expensive to copy, you can consider a vector of pointers
instead of objects; it makes the code a bit more complicated, but you
still get the benefit of automatic vector resizing as you add more
elements. And if you ever need to, say, sort the collection, that ends up
being a lot more efficient when you have an array of pointers than when you
have an array of objects.

Many folks these days would counsel you to just use a vector in any case.
In your case, I'd tend to agree with that. Old-style "dynamic arrays" are
becoming more of a special-case than the general rule. They have their
place when performance considerations force the issue, but vectors (and
other STL containers) are much more pleasant to code with, and lead to more
robust programs.
-leor
 
J

John Harrison

James said:
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; // 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,168
Messages
2,570,914
Members
47,455
Latest member
Delilah Code

Latest Threads

Top