# Thread: Array of pointers to point objects

1. ## Array of pointers to point objects

hi
how do i create an array of pointers to point objects. point objects are delared by the following.
I dont know how many point objects i will be taking in so how do deal with this and how do i reference each point on the array afterwards!!!!
any help much appreciated...............
Code:
```class point {
point::point() {}
point::point(int int);
point::getX();
point::getY();
protected:
float x;
float y;
}; //end class point```
i have functions that get x and y, i need to be able to reference them for calculating the area of a triangle......

2. I'm a bit rusty on C++, but I think that this code should work.

Code:
```int numPoints = 10;
Point **array = new Point*[numPoints];

for( int i = 0; i < 10; i++ )
{
array[i] = new Point( i, i );
}

cout << array[6]->getX();

//Free the memory
for( int i = 0; i < 10; i++ )
{
delete array[i];
}

delete [] array;```

3. >I dont know how many point objects i will be taking in
Use a container class (std::vector for example). It makes life much easier when your arrays must be dynamic.

4. In fact, a std::vector of std::auto_pointers would make things a whole lot easier, and less prone to memory leaks.

5. >In fact, a std::vector of std::auto_pointers would make things a whole lot easier
No, actually it would make things a lot more difficult because the copy semantics of std::auto_ptr doesn't match the copy requirements of STL container classes. Don't use std::auto_ptr in standard container classes. If you must use a smart pointer (which is usually a good idea), use the smart pointer offered by the boost library.

6. thanks v much...i figured out it was a vector...and use the push_back function.....
another problem im having is taking int from a file in the form of a string, the string contains x,y coords for a point and the int is the number of point coords in the string array....how do i pass these x,y values from the string array to different point objects and then pass on every 3 points i get to a triangle member fun which calculates the area of the triangle.....

i hav a polygon class which contains an array of coords and a no of points int, i want to pass the file data to these and then pass on to point objects then get the area......i already have these functions done

here is the header file for it.....
Code:
```#if !defined(FUTILSHH)
#define FUTILSHH

const char *getFilename(int, const char **);
int countPolyPoints(const char *);

#endif```

7. I'm not quite sure what you're asking. Do you want to convert a string formatted as x,y to two integers and make a point object out of that? If so you can use stringstream to parse the input into ints:
Code:
```#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct point {
int x;
int y;

point()
: x ( 0 )
, y ( 0 )
{}
point ( int x, int y )
: x ( x )
, y ( y )
{}
};

int main()
{
string s ( "12,36" );
istringstream iss ( s );
int new_x;
int new_y;

iss>> new_x;
iss.ignore();
iss>> new_y;

point new_point ( new_x, new_y );

cout<< new_point.x <<' '<< new_point.y;
}```