object arrays

J

john townsley

I want to know how can you have an array of objects declared if you dont
know how many items will be in the array?

eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command where you preserve the existing
array and just add another index. Do you need to go through contatiners?

thanks
 
S

Shan

john said:
I want to know how can you have an array of objects declared if you dont
know how many items will be in the array?

eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command where you preserve the existing
array and just add another index. Do you need to go through contatiners?

thanks

Use vector which can hold any object of the same type and can grow
dynamically. Checkout the STL to use the vector.
Regards
Shan
 
M

Mike Wahler

john townsley said:
I want to know how can you have an array of objects declared if you dont
know how many items will be in the array?

Allocate the array with operator 'new[]'
(and don't forget to deallocate it with
operator 'delete[]' when you're done with
the array.

But imo the superior alternative is to stop
using an array and use a container instead
(e.g. vector).
eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command

C++, 'new/delete' doesn't have that capability. You'd
need to allocate a new array of the new size, copy the
old array elements to it, then delete the old array.
where you preserve the existing
array and just add another index.

Do you need to go through contatiners?

You don't *have* to, but I reccomend it.

A container can be 'grown' or 'shrunk' as needs dictate.

-Mike
 
J

john townsley

Mike Wahler said:
john townsley said:
I want to know how can you have an array of objects declared if you dont
know how many items will be in the array?

Allocate the array with operator 'new[]'
(and don't forget to deallocate it with
operator 'delete[]' when you're done with
the array.

But imo the superior alternative is to stop
using an array and use a container instead
(e.g. vector).
eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command

C++, 'new/delete' doesn't have that capability. You'd
need to allocate a new array of the new size, copy the
old array elements to it, then delete the old array.
where you preserve the existing
array and just add another index.

Do you need to go through contatiners?

You don't *have* to, but I reccomend it.

A container can be 'grown' or 'shrunk' as needs dictate.

-Mike

so using a container like vector is the standard way of a list of Objects of
unknown size....
also with containers is it more of a standard to hold the pointers to
objects or the objects themselves.
 
S

Shan

john said:
Mike Wahler said:
john townsley said:
I want to know how can you have an array of objects declared if you dont
know how many items will be in the array?

Allocate the array with operator 'new[]'
(and don't forget to deallocate it with
operator 'delete[]' when you're done with
the array.

But imo the superior alternative is to stop
using an array and use a container instead
(e.g. vector).
eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command

C++, 'new/delete' doesn't have that capability. You'd
need to allocate a new array of the new size, copy the
old array elements to it, then delete the old array.
where you preserve the existing
array and just add another index.

Do you need to go through contatiners?

You don't *have* to, but I reccomend it.

A container can be 'grown' or 'shrunk' as needs dictate.

-Mike

so using a container like vector is the standard way of a list of Objects of
unknown size....
also with containers is it more of a standard to hold the pointers to
objects or the objects themselves.


It is upto you to decide. The common way is to hold the objects
themselves.
 
R

Richard Herring

Shan said:
john said:
Mike Wahler said:
I want to know how can you have an array of objects declared if you dont
know how many items will be in the array?

Allocate the array with operator 'new[]'
(and don't forget to deallocate it with
operator 'delete[]' when you're done with
the array.

But imo the superior alternative is to stop
using an array and use a container instead
(e.g. vector).


eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command

C++, 'new/delete' doesn't have that capability. You'd
need to allocate a new array of the new size, copy the
old array elements to it, then delete the old array.

where you preserve the existing
array and just add another index.


Do you need to go through contatiners?

You don't *have* to, but I reccomend it.

A container can be 'grown' or 'shrunk' as needs dictate.

so using a container like vector is the standard way of a list of Objects of
unknown size....

Yes, but don't call it a "list", which is a different kind of container
;-)
It is upto you to decide. The common way is to hold the objects
themselves.
.... provided they meet the container's requirements for contained
objects (basically, that you can take copies of them.)
 
R

red floyd

Richard said:
Shan said:
john said:
I want to know how can you have an array of objects declared if

you dont
know how many items will be in the array?

Allocate the array with operator 'new[]'
(and don't forget to deallocate it with
operator 'delete[]' when you're done with
the array.

But imo the superior alternative is to stop
using an array and use a container instead
(e.g. vector).


eg MyClass Myobject[?];
or
MyClass *Myobject[?]

In VB you could simply have a Redim command

C++, 'new/delete' doesn't have that capability. You'd
need to allocate a new array of the new size, copy the
old array elements to it, then delete the old array.

where you preserve the existing
array and just add another index.


Do you need to go through contatiners?

You don't *have* to, but I reccomend it.

A container can be 'grown' or 'shrunk' as needs dictate.

so using a container like vector is the standard way of a list of

Objects of
unknown size....


Yes, but don't call it a "list", which is a different kind of container ;-)
It is upto you to decide. The common way is to hold the objects
themselves.
... provided they meet the container's requirements for contained
objects (basically, that you can take copies of them.)

And that you don't need polymorphic behavior. If you need polymorphic
behavior, you have to use a container of (hopefully smart) pointers.
 

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,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top