Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:41 PM
SB
Guest
 
Posts: n/a
Default How can I do this?

I have a .h file containing two classes, one called Player, the other called
Cards. I have, of course a seperate .cpp file containing the class
implementations, etc. Also, I have another .cpp file which just contains
main(). My problem is that in main I have an array of class Player, such as
Players allPlayers[10]; This of course calls the Player constructor ten
times, which is fine. However, I don't have access to the allPlayers array
inside the Player class in the .cpp implementation file. I really need to
have access to an array of class Player inside the class implementation
itself. How can I do this?

Thanks!
SB


  #2  
Old July 19th, 2005, 08:41 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: How can I do this?

"SB" <voador79@hotmail.com> wrote...[color=blue]
> I have a .h file containing two classes, one called Player, the other[/color]
called[color=blue]
> Cards. I have, of course a seperate .cpp file containing the class
> implementations, etc. Also, I have another .cpp file which just contains
> main(). My problem is that in main I have an array of class Player, such[/color]
as[color=blue]
> Players allPlayers[10]; This of course calls the Player constructor ten
> times, which is fine. However, I don't have access to the allPlayers array
> inside the Player class in the .cpp implementation file. I really need to
> have access to an array of class Player inside the class implementation
> itself. How can I do this?[/color]

The most _trivial_ way would be to pass a pointer to the first element
of that array to the [member] function where you need to access the
elements.

Another way could be to give each "Player" object a hint as to the fact
that it's in an array by setting some data member variable to the index
of the object in that array. Using that index and the 'this' pointer
the object could extrapolate the other array elements.

I bet there are other ways (numerous, although not countless).

Victor


  #3  
Old July 19th, 2005, 08:42 PM
jeffc
Guest
 
Posts: n/a
Default Re: How can I do this?


"SB" <voador79@hotmail.com> wrote in message
news:dg0nb.115500$a16.76857@lakeread01...[color=blue]
> I have a .h file containing two classes, one called Player, the other[/color]
called[color=blue]
> Cards. I have, of course a seperate .cpp file containing the class
> implementations, etc. Also, I have another .cpp file which just contains
> main(). My problem is that in main I have an array of class Player, such[/color]
as[color=blue]
> Players allPlayers[10]; This of course calls the Player constructor ten
> times, which is fine. However, I don't have access to the allPlayers array
> inside the Player class in the .cpp implementation file. I really need to
> have access to an array of class Player inside the class implementation
> itself. How can I do this?[/color]

Right off the top of my head, I can't understand why you'd want that. If
you explain why, I might be able to tell you how to accomplish what you
want. Normally a class should not know anything about how that class is
used in a data structure, for example (other than the fact that it is
designed to do so, it shouldn't have any knowledge of the actual
implementation of it.)


  #4  
Old July 19th, 2005, 08:42 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: How can I do this?

On Sun, 26 Oct 2003 22:27:16 -0500, "SB" <voador79@hotmail.com> wrote:
[color=blue]
>I have a .h file containing two classes, one called Player, the other called
>Cards. I have, of course a seperate .cpp file containing the class
>implementations, etc. Also, I have another .cpp file which just contains
>main(). My problem is that in main I have an array of class Player, such as
>Players allPlayers[10]; This of course calls the Player constructor ten
>times, which is fine. However, I don't have access to the allPlayers array
>inside the Player class in the .cpp implementation file. I really need to
>have access to an array of class Player inside the class implementation
>itself. How can I do this?
>
>Thanks!
>SB
>
>[/color]

Declare the array "extern" in the .cpp file.


--
Bob Hairgrove
NoSpamPlease@Home.com
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles