473,568 Members | 2,898 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 1547

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**********@o ptusnet.com.au> wrote in message
news:42******** **************@ news.optusnet.c om.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******@mkwah ler.net> wrote in message
news:Ih******** *********@newsr ead3.news.pas.e arthlink.net...

"john townsley" <jo**********@o ptusnet.com.au> wrote in message
news:42******** **************@ news.optusnet.c om.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******@mkwah ler.net> wrote in message
news:Ih******** *********@newsr ead3.news.pas.e arthlink.net...

"john townsley" <jo**********@o ptusnet.com.au> wrote in message
news:42******** **************@ news.optusnet.c om.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************ **********@c13g 2000cwb.googleg roups.com>, Shan
<sh*******@redi ffmail.com> writes

john townsley wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:Ih******** *********@newsr ead3.news.pas.e arthlink.net...
>
> "john townsley" <jo**********@o ptusnet.com.au> wrote in message
> news:42******** **************@ news.optusnet.c om.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************ **********@c13g 2000cwb.googleg roups.com>, Shan
<sh*******@redi ffmail.com> writes

john townsley wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:Ih******** *********@newsr ead3.news.pas.e arthlink.net...
>
> "john townsley" <jo**********@o ptusnet.com.au> wrote in message
> news:42******** **************@ news.optusnet.c om.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.n et>, red floyd
<no*****@here.d ude> writes
Richard Herring wrote:
In message <11************ **********@c13g 2000cwb.googleg roups.com>,
Shan <sh*******@redi ffmail.com> writes

john townsley wrote:

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:Ih******** *********@newsr ead3.news.pas.e arthlink.net...
>
> "john townsley" <jo**********@o ptusnet.com.au> wrote in message
> news:42******** **************@ news.optusnet.c om.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 CopyConstructib le 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.a u>, john
townsley <jo**********@o ptusnet.com.au> writes

"Shan" <sh*******@redi ffmail.com> wrote in message
news:11******* *************** @c13g2000cwb.go oglegroups.com. ..

john townsley wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:Ih******** *********@newsr ead3.news.pas.e arthlink.net...
>
> "john townsley" <jo**********@o ptusnet.com.au> wrote in message
> news:42******** **************@ news.optusnet.c om.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
10677
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 (!in_array($obj, $result)) array_push($result, $obj); } }
2
18663
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 something like following: ------------------------ | A | B | C | Null | D | ------------------------ | R |
9
4771
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not...
16
25399
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
38
5166
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 let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of...
3
4625
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 passed as input parameters to the DLL through the SetParams method defined as follows: interface ICliquet : IPricable
6
1613
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 class takes a lot of storage or other resources? Would it be a severe performance penalty? From the .Net help doc:
4
3697
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for...
14
2786
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 shows all the properties, weeks available, pricing, etc. for a particular area of Europe. The data varies widely depending on the region; at times...
14
1967
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 convert an object of this type to the following type?
0
7916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.