0

I have a program that creates a random amount of points interspersed throughout the program. While it runs, I would also like to create an object for each point and store it in a vector. I have created a Point class with various attributes but I have no idea on how to implement the above. When looking at other questions that deal with similar, yet nonidentical problems, pointers are used, but again, I have no idea on how to implement them.

1
  • What is the first problem you run into when you try to implement it? Commented Jul 8, 2012 at 18:55

3 Answers 3

1

Im not quite sure what you really want to achieve, but i hope this will help you though.

To create an object dynmically use the new operator. The new operator always returns a pointer:

Point* pointObj = new Point();

If you have specified a constructor the call is very similar to normal construction on stack:

Point* pointObj = new Point(x,y);

A std::vector stores objects at runtime (dynamically in the heap), but instead of creating them by it own it simply copies them:

std::vector<Point> vec; //if this object is destructed it contents are destructed aswell

Point pointObj(x,y); //point on stack; will get destructed if it gets out of scope
vec.push_back(pointObj) //copy pointObj to a dynamic location on the heap
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget delete for anything created with new!
0

Well, I don't know what parameters your Point constructor takes, but your description sounds as if you want to do something like this:

std::vector<Point> MyGlobalPointList;

and inside your program you have a few of these:

MyGlobalPointList.push_back(Point(x,y,color));

Comments

0

Are you looking for automatic object management tied with object creation here? If so, AbstractFactory can help you here. Apart from the factory being THE mechanism for constructing objects (Points) instead of doing so everywhere yourself, it can also carry out object management e.g. managing them in a vector.

class Point {
  friend class PointFactory;
  Point(int _x, int _y) : x(_x), y(_y) { }
  private:
    ~Point();    //destructor is private
    int x, y;
}

class PointFactory {
  public:
    Point* createPoint() {    //Creates random point
      return createPoint(rand(), rand());
    }
    Point* createPoint(int x, int y) {    //Creates specified point
      Point* p = new Point(x, y);
      points.push_back(p);
      return p;
    }
    void deletePoint(Point *p) {    //p not in use anymore
      std::vector<Point*>::iterator it = std::find(objects.begin(), objects.end(), p);
      if (it != objects.end()) {
        objects.erase(it);
      }
      delete p;
    }
  private:
    std::vector<Point*> objects;
}

int main(...) {
  Point *p = pointFactory.createPoint();    //instead of new Point()
  //use p
  pointFactory.deletePoint(p);    //p not in use anymore
  return 0;
}

Hope this is what you are looking for.

  • Ankur Satle

1 Comment

This is not very useful IMO. A std::vector<Point> (not std::vector<Point*> is nearly the exact same as your wrapper class. The only difference is the missing creation of objects but since there is no requirement for this and you "factory" is not implemented polymorphic i would say there is indeed no difference except much boilerplate code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.