digitalmars.D.learn - Problem with classes
- Essoje Oliveira de Almeida (29/29) Feb 18 2006 I'm having a bit of a problem when trying to do the following.
- Jarrett Billingsley (22/53) Feb 18 2006 It took me a second to figure out what's going on here, but it's kind of...
- Essoje Oliveira de Almeida (2/22) Feb 18 2006 I see. That did clear up things for me, thank you very much. :)
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
"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
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








Essoje Oliveira de Almeida <Essoje_member pathlink.com>