How can I do this?

S

SB

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
 
V

Victor Bazarov

SB said:
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?

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
 
J

jeffc

SB said:
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?

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.)
 
B

Bob Hairgrove

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top