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 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
"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
"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.
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.
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
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
}
}
|
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 |
|
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...
|
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?
|
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...
| |
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
|
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:
|
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...
|
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...
|
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?
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |