#### Archived

This topic is now archived and is closed to further replies.

# How Do I Create Multiple Objects Without Declaring Them, With A Loop?

This topic is 5015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello All!, I am creating a loop that creates 6 clocks with random time zones. What I am unclear on is this: How do I create multiple objects without actually declaring them? I am trying to have a loop do it all for me. Do I do it by this?: Clock clock[y] ???? How do I add a letter or number to the end of the name of the object so it differs from the other? Thanks!
    for ( int y = 0; y < 7; y++){
Clock clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock.draw(page);
}

[edited by - Klear on March 24, 2004 5:02:34 PM]

##### Share on other sites
Clock* clocks[7];for ( int y = 0; y < 7; y++){        clocks[y] = new Clock(timeZoneIds[random], xplace, yplace);xplace += 100;yplace += 100;clock.draw(page);    }

##### Share on other sites
Thanks for the reply, but I am getting errors...

ClockDriver.java [52:1] not a statement      Clock* clocks[7];           ^1 errorErrors compiling ClockDriver.

[edited by - Klear on March 24, 2004 5:17:37 PM]

##### Share on other sites
That might be because the replier posted C++ code?!

DISCLAIMER: If any of the above statements are incorrect, feel free to deliver me a good hard slap!

My games: DracMan | Swift blocks

##### Share on other sites
Clock ** clocks

clocks = new (Clock *)[7];

for(i=0;i<7;++i)
clocks = new Clock();

for(i=0;i<7;++i)
delete clocks[i];

delete[] clocks;

##### Share on other sites
Hmm yea your prolly right. = / This is Java source btw

##### Share on other sites
That s because you are using java and peter_b thought you used c++.

Clock clock[7];

for ( int y = 0; y < 7; y++){
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock[y].draw(page);
}

LizardCPP

[edited by - LizardCPP on March 24, 2004 5:24:03 PM]

##### Share on other sites
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.

 Clock clock[7];for ( int y = 0; y < 7; y++){ clock[y] = new Clock(timeZoneIds[random], xplace, yplace); xplace += 100; yplace += 100; clock[y].draw(page); }

LizardCPP

Thanks man but...

ClockDriver.java [52:1] '']'' expected      Clock clock[7];                  ^1 errorErrors compiling ClockDriver.

##### Share on other sites
quote:
Original post by Klear
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.

 Clock clock[7];for ( int y = 0; y < 7; y++){ clock[y] = new Clock(timeZoneIds[random], xplace, yplace); xplace += 100; yplace += 100; clock[y].draw(page); }

LizardCPP

Thanks man but...

ClockDriver.java [52:1] ']' expected      Clock clock[7];                  ^1 errorErrors compiling ClockDriver.

ah thats right....

you have to do Clock clock[] = new Clock[7]; first then the for-statement.

[edited by - LizardCPP on March 24, 2004 5:28:13 PM]

##### Share on other sites
quote:
Original post by LizardCPP
quote:
Original post by Klear
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.

 Clock clock[7];for ( int y = 0; y < 7; y++){ clock[y] = new Clock(timeZoneIds[random], xplace, yplace); xplace += 100; yplace += 100; clock[y].draw(page); }

LizardCPP

Thanks man but...

ClockDriver.java [52:1] ']' expected      Clock clock[7];                  ^1 errorErrors compiling ClockDriver.

ah thats right....

you have to do Clock clock = new Clock[7]; first then the for-statement.

Thanks for hanging in Lizard Now its saying that I need an array? Are the brackets not doing that?

   ClockDriver.java [52:1] incompatible typesfound   : Clock[]required: Clock      Clock clock = new Clock[7];                    ^ClockDriver.java [55:1] array required, but Clock found        clock[y] = new Clock(timeZoneIds[random], xplace, yplace);             ^ClockDriver.java [58:1] array required, but Clock found        clock[y].draw(page);             ^3 errorsErrors compiling ClockDriver.

[edited by - Klear on March 24, 2004 5:28:41 PM]

[edited by - Klear on March 24, 2004 5:29:22 PM]

##### Share on other sites
you didn''t copy his code right:

Clock clock[] = new Clock[7];

NOT

Clock clock = new Clock[7];

-me

##### Share on other sites
hah thanks. I just jump the gun all the time when I am into something Thanks For all the help.

##### Share on other sites
quote:
Original post by Anonymous Poster
Clock ** clocks

clocks = new (Clock *)[7];

for(i=0;i<7;++i)
clocks = new Clock();

This is so ugly, why a double pointer, newing an array with a cast etc ?
A class is stored as a pointer anyway, so why this indirection ? Why not just

Clock clocks[7];
for(i=0;i<7;i++) {
clocks[i] = new Clock();
}

##### Share on other sites
quote:
Original post by Fidelio66

This is so ugly, why a double pointer, newing an array with a cast etc ?
A class is stored as a pointer anyway, so why this indirection ? Why not just

Clock clocks[7];
for(i=0;i<7;i++) {
clocks = new Clock();
}

void DoStuffL(){  RPointerArray< Clock > iClocks;  for ( TInt i = 0; i < 7; ++i )  {    Clock *iClock = Clock::NewL();    CleanupStack:: Push( iClock );    iClocks.Append( iClock );  }  for ( TInt i = 0; i < 7; ++i ) CleanupStack:: Pop();}

(I'm sure someone who actually knows symbian can point out quite a few mistakes with that code. And I thought microsoft's code looked bad..)

[edited by - Painless on March 24, 2004 7:06:37 PM]

##### Share on other sites
Why can't you just do
Clock clocks[7]?

Oh, you have a non-trivial ctor, that's why. Evil, pure evil.

[edited by - Magmai Kai Holmlor on March 24, 2004 8:59:15 PM]

##### Share on other sites
quote:

void DoStuffL()

{snip}

You want the stability, you gotta pay the price....

[edited by - shmoove on March 25, 2004 4:22:59 AM]