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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Thanks for the reply, but I am getting errors...


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


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 error
Errors compiling ClockDriver.

Share this post


Link to post
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 error
Errors 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 this post


Link to post
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 error
Errors 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 types
found : 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 errors
Errors 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 this post


Link to post
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 this post


Link to post
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();
}



Ugly? UGLY?! Well, how about THIS!


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 this post


Link to post
Share on other sites
quote:

Ugly? UGLY?! Well, how about THIS!

void DoStuffL()

{snip}


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

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

Share this post


Link to post
Share on other sites