|
-
Nov 30th, 2002, 03:11 PM
#1
Thread Starter
Addicted Member
Initialising arrays of objects
Me again.
When you create an array of objects, how can you pass the arguments required by the constructor?
example:
when you declare a single object it might look something like this:
Code:
clsComplex Number(2,1);
but when you declare an array of objects:
Code:
clsComplex Number[3];
How do you also add the arguments for the constructors?
Using Visual Studio .NET 2005
-
Nov 30th, 2002, 07:24 PM
#2
Junior Member
You cannot do it that way.
You have to allocate You objects at run-time (on heap) through new:
Code:
clsComplex* ppComplex[3];
for (int x=0; x < 3;++x)
{
ppComplex[x] = new clsComplex(2,1); // call ctor with arguments
}
Don't forget to delete Your array-ptrs. after use!
Mikey
Last edited by MikeyD; Nov 30th, 2002 at 07:36 PM.
-
Nov 30th, 2002, 09:37 PM
#3
Monday Morning Lunatic
I don't know, it should be possible:
Code:
object blah[3] = { object(4,5), object(2, 89), object(69, 31337) };
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 1st, 2002, 08:49 AM
#4
Thread Starter
Addicted Member
Using Visual Studio .NET 2005
-
Dec 1st, 2002, 09:11 AM
#5
Monday Morning Lunatic
Note that the first is more flexible, while the second is more efficient (and you don't need to delete afterwards).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 1st, 2002, 10:33 AM
#6
Thread Starter
Addicted Member
Just as an addition, since it's kind of related:
What's up with this:
Code:
BYTE bArray[5];
bArray={1,2,3,4,5};
I can't use this "BYTE bArray[5]={1,2,3,4,5};" because the array values will changes.
Just to be specific, the code should assign 1 to bArray[0], 2 to bArray[1] etc.
Using Visual Studio .NET 2005
-
Dec 1st, 2002, 10:37 AM
#7
Monday Morning Lunatic
You can use the second one, since you can still change the array contents.
It's only impossible to change it if you declare the array as const.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 1st, 2002, 10:46 AM
#8
Junior Member
parksie is right,
You only need the new-op. if You have to decide at runtime, how large Your array is
or if You have to change the array-size.
Mikey
-
Dec 1st, 2002, 10:46 AM
#9
Thread Starter
Addicted Member
errr, how?
(I know I'm a pain)
Using Visual Studio .NET 2005
-
Dec 1st, 2002, 10:47 AM
#10
Junior Member
-
Dec 1st, 2002, 10:55 AM
#11
Junior Member
if You mean with "how", how to assign new values to Your array:
Code:
BYTE bArray[5] = {1,2,3,4,5};
bArray[3] = 6; // assign a new value to index 3
-
Dec 1st, 2002, 10:55 AM
#12
Thread Starter
Addicted Member
I was replying to parksie's , sorry
I wanted to know how I could change the contents of the array using the method you both describe.
Using Visual Studio .NET 2005
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|