1

I'm unfamiliar with OO in C++.

I've been pushing instances of the MyPoint class into

vector <MyPoint> trianglePoints;

like this:

trianglePoints.push_back(MyPoint(x,y));

This is my definition of MyPoint:

class MyPoint {

public:
    float x; 
    float y;

    MyPoint(float x, float y) //constructor
    {
        this->x=x;
        this->y=y;

    }

}; //end

After pushing three points into the vector I call a function to render the triangle and then do:

trianglePoints.clear();

Questions:

a) How do I get my three x,y coordinates from the vector? I want to store each into its own int xi,yi to render them.

b) Is it okay to call clear() on the vector even though I haven't defined a destructor for the MyPoint class?

andandandand
  • 21,946
  • 60
  • 170
  • 271
  • 1
    If you don't have one, make sure you get [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list); it will help to answer this sort of question for you. – James McNellis Oct 16 '10 at 00:50
  • @James: You sound like me now, in this regard. :) Good, I was getting tired of being the one saying "get a book". :P – GManNickG Oct 16 '10 at 00:51
  • 1
    I think asking in SO is way faster (and more educational and thought-clarifying) than going through a book. – andandandand Oct 16 '10 at 00:56
  • 1
    @omgzor: Ha, no. You'll never learn it properly if you scrap for bits of information online. Also, use an initialization list. – GManNickG Oct 16 '10 at 01:09
  • 2
    @GMan: Heh. Well, now that we don't have Mr. Butterworth to keep the beginners in line, I figure we all have to help out as much as we can :-D – James McNellis Oct 16 '10 at 01:20
  • @James: Yeah, it'll take 2 or 3 of us to equal up. :) – GManNickG Oct 16 '10 at 01:51

3 Answers3

2

a)

trianglePoints[0].x
trianglePoints[0].y
trianglePoints[1].x
trianglePoints[1].y
trianglePoints[2].x
trianglePoints[2].y

b)

Yes, the class uses the default destructor.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
2

(a) You can get elements from the vector using array syntax:

for (int i = 0; i < 3; i++)
{
    do_something(trianglePoints[i]);
}

or iterator syntax:

for (std::vector<MyPoint>::iterator it = trianglePoints.begin();
     it != trianglePoints.end(); ++it)
{
    do_something(*it);
}

or using algorithm syntax:

std::for_each(trianglePoints.begin(), trianglePoints.end(); do_something);

For more details on what you can do with std::vector, see e.g. http://cplusplus.com/reference/stl/vector/.

(b) It's ok to call clear; explicit destructors are only necessary if you have resources to clear up (like memory, file handles, etc.), or if your class is a base class (in which case you may need a virtual destructor).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

How do I get my three x,y coordinates from the vector?

vector overloads the subscript operator ([]), so elements can be accessed as if they are in an array. Alternatively (and preferably), you can use iterators.

Is it okay to call clear() on the vector even though I haven't defined a destructor for the MyPoint class?

Yes; the compiler provides a default destructor.

James McNellis
  • 348,265
  • 75
  • 913
  • 977