By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

object arrays

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

john townsley wrote:
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

Jul 23 '05 #2

P: n/a

"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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
Jul 23 '05 #3

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ih*****************@newsread3.news.pas.earthl ink.net...

"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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.

Jul 23 '05 #4

P: n/a

john townsley wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ih*****************@newsread3.news.pas.earthl ink.net...

"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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.

Jul 23 '05 #5

P: n/a
In message <11**********************@c13g2000cwb.googlegroups .com>, Shan
<sh*******@rediffmail.com> writes

john townsley wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ih*****************@newsread3.news.pas.earthl ink.net...
>
> "john townsley" <jo**********@optusnet.com.au> wrote in message
> news:42**********************@news.optusnet.com.au ...
>> I want to know how can you have an array of objects declared ifyou 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
;-)
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.

.... provided they meet the container's requirements for contained
objects (basically, that you can take copies of them.)

--
Richard Herring
Jul 23 '05 #6

P: n/a
Richard Herring wrote:
In message <11**********************@c13g2000cwb.googlegroups .com>, Shan
<sh*******@rediffmail.com> writes

john townsley wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ih*****************@newsread3.news.pas.earthl ink.net...
>
> "john townsley" <jo**********@optusnet.com.au> wrote in message
> news:42**********************@news.optusnet.com.au ...
>> 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 ;-)
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.

... 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.
Jul 23 '05 #7

P: n/a
In message <HR*****************@newssvr24.news.prodigy.net> , red floyd
<no*****@here.dude> writes
Richard Herring wrote:
In message <11**********************@c13g2000cwb.googlegroups .com>,
Shan <sh*******@rediffmail.com> writes

john townsley wrote:

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ih*****************@newsread3.news.pas.earthl ink.net...
>
> "john townsley" <jo**********@optusnet.com.au> wrote in message
> news:42**********************@news.optusnet.com.au ...
>> 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 ;-)
also with containers is it more of a standard to hold the pointers

objects or the objects themselves.
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.


That's implicit in the CopyConstructible requirement. A (base class)
copy of a derived object wouldn't be equivalent to it.
If you need polymorphic behavior, you have to use a container of
(hopefully smart) pointers.


Seconded.

--
Richard Herring
Jul 23 '05 #8

P: n/a
In message <42**********************@news.optusnet.com.au>, john
townsley <jo**********@optusnet.com.au> writes

"Shan" <sh*******@rediffmail.com> wrote in message
news:11**********************@c13g2000cwb.googleg roups.com...

john townsley wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ih*****************@newsread3.news.pas.earthl ink.net...
>
> "john townsley" <jo**********@optusnet.com.au> wrote in message
> news:42**********************@news.optusnet.com.au ...
>> 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.


i am confused, arent you wasting memory


Is memory rationed now? Don't worry about "wasting" it until you *know*
you have a problem.
by not declaring Pointers to Global
objects of unknown size? and simply declaring the instances and copy to
container?


I don't follow. Why are your objects of unknown size, and what do you
mean by "global" here?

If you mean you want a *heterogeneous* container of different sorts of
objects, not a single uniform type, then you can't directly store them
in a standard container. If they are polymorphic objects derived from a
common base class, store smart pointers. If they are some kind of data
tagged with type information, there are various kinds of "variant" class
that might fit the bill. If they are totally unrelated, why would you
want to keep them in the same container?

--
Richard Herring
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.