Thread: Array of pointers to point objects

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    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......
    Last edited by totalfreeloader; 11-26-2003 at 02:29 PM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In fact, a std::vector of std::auto_pointers would make things a whole lot easier, and less prone to memory leaks.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    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. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  3. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM