www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with classes

reply Essoje Oliveira de Almeida <Essoje_member pathlink.com> writes:
I'm having a bit of a problem when trying to do the following.

Here's the pseudo-code
**************************
int main()
{
Mobile NPC[2] = new Mobile;

NPC[0].GivenName[]  = "Some         ";
NPC[0].FamilyName[] = "Name         ";
NPC[0].NickName[]   = "Random NPC   ";

NPC[1].GivenName[]  = "Medusa       ";
NPC[1].FamilyName[] = "Gorgon       ";
NPC[1].NickName[]   = "Beheaded Lady";

PC.PrintInfo();
NPC[0].PrintInfo();
NPC[1].PrintInfo();
return 0;
}
**************************

And here's the output I'd get from it.
**************************
Name: Medusa        Gorgon
Nick: Beheaded Lady

Name: Medusa        Gorgon
Nick: Beheaded Lady
**************************

What kind of approach I'd need to use for NPC[0] and NPC[1] to be treated as two
separated entities, thus enabling me to create as much class instances as needed
at runtime?

Thanks in advance.
Feb 18 2006
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Essoje Oliveira de Almeida" <Essoje_member pathlink.com> wrote in message 
news:dt7nqv$2ioi$1 digitaldaemon.com...
 I'm having a bit of a problem when trying to do the following.

 Here's the pseudo-code
 **************************
 int main()
 {
 Mobile NPC[2] = new Mobile;

 NPC[0].GivenName[]  = "Some         ";
 NPC[0].FamilyName[] = "Name         ";
 NPC[0].NickName[]   = "Random NPC   ";

 NPC[1].GivenName[]  = "Medusa       ";
 NPC[1].FamilyName[] = "Gorgon       ";
 NPC[1].NickName[]   = "Beheaded Lady";

 PC.PrintInfo();
 NPC[0].PrintInfo();
 NPC[1].PrintInfo();
 return 0;
 }
 **************************

 And here's the output I'd get from it.
 **************************
 Name: Medusa        Gorgon
 Nick: Beheaded Lady

 Name: Medusa        Gorgon
 Nick: Beheaded Lady
 **************************

 What kind of approach I'd need to use for NPC[0] and NPC[1] to be treated 
 as two
 separated entities, thus enabling me to create as much class instances as 
 needed
 at runtime?

 Thanks in advance.

It took me a second to figure out what's going on here, but it's kind of tricky and has to do with D's array semantics. When you define the array: Mobile NPC[2] = new Mobile; Something interesting happens. NPC is a statically-sized array of length 2 (i.e. a Mobile[2]). When you initialize it to "new Mobile," it assigns _all_ the elements of NPC to the same instance of Mobile. Thus, NPC[0] and NPC[1] both point to the same instance of Mobile. This is a useful feature of D's arrays as it allows you to fill an array with a single value very quickly, but in this case, it's not immediately obvious. Instead, do Mobile[2] NPC; foreach(inout Mobile m; NPC) m = new Mobile; That will create a new instance of Mobile for each element in NPC. Notice the "inout" in the foreach loop; that makes it so you can modify the contents of NPC inside the foreach loop through the variable m. This is equivalent to for(int i = 0; i < NPC.length; i++) NPC[i] = new Mobile;
Feb 18 2006
parent Essoje Oliveira de Almeida <Essoje_member pathlink.com> writes:
In article <dt7r0k$2l0r$1 digitaldaemon.com>, Jarrett Billingsley says...
It took me a second to figure out what's going on here, but it's kind of 
tricky and has to do with D's array semantics.

When you define the array:

Mobile NPC[2] = new Mobile;

Something interesting happens.  NPC is a statically-sized array of length 2 
(i.e. a Mobile[2]).  When you initialize it to "new Mobile," it assigns 
_all_ the elements of NPC to the same instance of Mobile.  Thus, NPC[0] and 
NPC[1] both point to the same instance of Mobile.  This is a useful feature 
of D's arrays as it allows you to fill an array with a single value very 
quickly, but in this case, it's not immediately obvious.

Instead, do

Mobile[2] NPC;
foreach(inout Mobile m; NPC)
    m = new Mobile;

That will create a new instance of Mobile for each element in NPC.  Notice 
the "inout" in the foreach loop; that makes it so you can modify the 
contents of NPC inside the foreach loop through the variable m.  This is 
equivalent to

for(int i = 0; i < NPC.length; i++)
    NPC[i] = new Mobile; 

I see. That did clear up things for me, thank you very much. :)
Feb 18 2006