GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2006, 06:37
Całeczka Całeczka is offline
New Member
 
Join Date: Dec 2006
Posts: 8
Całeczka is on a distinguished road

[C++] Array of pointers to class objects


Hello 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  
Old 03-Dec-2006, 08:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: [C++] Array of pointers to class objects


Quote:
Originally Posted by Całeczka
.

My problem is such that I need an array of pointers


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:
foo *fooArray[100]; // an array of 100 pointers to "foo" objects

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:
foo **fooPointer;
fooPointer = new foo *[10] // memory for an array of 10 pointers

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:
// example of allocating and using arrays of pointers
// to objects
//
#include <iostream>
using std::cout;
using std::endl;

//
// A trivial class to use for the example
//
class toy
{
    public:
        int x;
};


int main()
{
    toy **tPoint; // a pointer to a pointer to an object from the "toy" class
    tPoint = new toy *[33]; // use as an array of 33 pointers to "toy" objects
    toy *tArray[100];       // an array of 100 pointers to "toy" objects
    int i;
    for (i = 0; i < 5; i++) {
        tPoint[i] = new toy;
        tPoint[i]->x = 10*i;
        tArray[i] = new toy;
        tArray[i]->x = i;
    }
    for (i = 0; i < 5; i++) {
        cout << "tPoint[" << i << "]->x = " << tPoint[i]->x
             << ", tArray[" << i << "]->x = " << tArray[i]->x
             << endl;
    }
    for (i = 0; i < 5; i++) {
        delete tPoint[i];
        delete tArray[i];
    }
    delete [] tPoint;
  return 0;
}


Output:
Code:
tPoint[0]->x = 0, tArray[0]->x = 0 tPoint[1]->x = 10, tArray[1]->x = 1 tPoint[2]->x = 20, tArray[2]->x = 2 tPoint[3]->x = 30, tArray[3]->x = 3 tPoint[4]->x = 40, tArray[4]->x = 4


Regards,

Dave
  #3  
Old 03-Dec-2006, 12:05
Całeczka Całeczka is offline
New Member
 
Join Date: Dec 2006
Posts: 8
Całeczka is on a distinguished road

Re: [C++] Array of pointers to class objects


Cheers mate ! You are a legend !
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.