![]() |
|
#1
|
|||
|
|||
[C++] Array of pointers to class objectsHello everybody ! I am a polish student.
I have got problem with my program, and because I am not very good programmer I need some help I am making chess. In my program I have got 6 classes (that represent all kind of pieces), each of them inherit parameters form abstract class Figure. I have got as well class Pole, objects of this class contain information about all 64 squares of chessboard. There is as well structure that contain x and y coordinates of chessboard. All those above things are the "frame" of my program, and they can not be changed. Because it was accepted by my tutor. My problem is such that I need an array of pointers, and I have no idea how to declerate it (I am still learning C++). I need such array of pointers to class objects, that when user choose x and y coordinates I need to know what kind of piece (what object) was chosen. For example when program get from user coordinates (as an element of structure) a will know what pointer is needed and this pointer will represent proper object. Because at this moment i have got nested loops what are checking all objects x and y coordinates if they are the same like those from user, and this is really not optimal idea. Thanks for any help. Sorry if I have done language errors. |
|
#2
|
|||
|
|||
Re: [C++] Array of pointers to class objectsQuote:
If you have a class named "foo", then you can declare an array of pointers to objects of this type by this: CPP / C++ / C Code:
If you want to allocate memory for an array of pointers dynamically (using the new operator) you can do it like this: CPP / C++ / C Code:
Now in either case, you have something that can be considered to be an array of pointers. In order to be useful they have to point to something. (That is, you have to make the pointer point to an actual object that holds your data items.) For example: CPP / C++ / C Code:
Output: Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: [C++] Array of pointers to class objectsCheers mate ! You are a legend !
![]() |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array of objects of a managed class | Gix | .NET Forum | 2 | 20-Dec-2006 03:27 |
| a tester class and then some. | postage | Java Forum | 1 | 06-May-2006 15:48 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 09:25 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | C++ Forum | 2 | 19-Dec-2004 06:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The