473,503 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object arrays

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
8 1544

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

"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

"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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10666
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
2
18654
by: Asad Khan | last post by:
Hello, I am writing a method that returns an array as an Object. Initially there is a single dimension array Object X. However, I can insert other arrays as Objects in X, thus I can end up with...
9
4765
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
16
25390
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
38
5145
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
3
4621
by: Marco | last post by:
Hello there, We're new to C# and have noticed a strange behaviour which we don't yet understand. We wrote a COM DLL (in VC++ 6.0) which performs some calculation based on some double arrays...
6
1610
by: Gary Frank | last post by:
What are the ramifications if I were to instantiate an object tens of thousands of times and add them to an array? Or hundreds of thousands of times? Do you know if the act of instantiating a...
4
3692
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
14
2774
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
14
1957
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.