I'm trying to test how and where the data is located and destroyed. For the example code below:
A new point is created and returned with NewCartesian method. I know that it should be stored in heap. When it's pushed back into the vector, does the memory of the points content is copied into a new Point structure? Or is it stored as a reference the this pointer?
When the point is created, when is it destroyed? Do I need to destroy the points when I'm done with it? Or are they destroyed when they are not usefuly anymore? For example, if main was another function, the vector would not be useful when it's finished.
Depending on the answers above, when is it good to use the reference of objects? Should I use Point& p or Point p for the return of Point::NewCartesian?
#define _USE_MATH_DEFINES #include <iostream> #include <cmath> using namespace std; struct Point { private: Point(float x, float y) : x(x), y(y) {} public: float x, y; static Point NewCartesian(float x, float y) { return{ x, y }; } }; int main() { vector<Point> vectorPoint; for (int i = 0; i < 10000; i++) { Point& p = Point::NewCartesian(5, 10); vectorPoint.push_back( p ); // vectorPoint.push_back( Point::NewCartesian(5, 10) ); Point& p2 = Point::NewPolar(5, M_PI_4); } cout << "deneme" << endl; getchar(); return 0; }
Thank you for your help,
Cheers,