-1

I'm new programmer in C++ and I have some basic questions that I can't understand by looking online.

1) let's say I have an object that it's members are only primitives. in my program, I create the object using new. what should be in this object destructor ?

2) Now let's say I have list called lst that contains some objects as the object above (only primitive members). if at some point I do the following: lst[i] = new MyObject; (when lst[i] already has object in it).

should I delete the object that was in lst[i] before this assignment? if not what actually happens?

here is part of my code to be more concrete:

class Point {
public:
    Point();
    ~Point();
    string toString() const;
    void set(long int x, long int y);
    Point(long int x, long int y);

    long int getX() const;

    long int getY() const;

    // -----------operators--------------

    bool operator==(const Point& point) const;
    bool operator!=(const Point& point);
    bool operator<(const Point& point);

private:
    long int _x;
    long int _y;
};

This is my PointSet class which hold list of points:

class PointSet
{
public:
    PointSet();
    PointSet(const PointSet &init);
    ~PointSet();
    string toString() const;
    bool add(const Point &point);
    bool remove(const Point &point);
    int getSize() const;
    void setSize(int newSize);
    bool operator==(const PointSet& other);
    bool operator!=(const PointSet& other);
    PointSet& operator-(const PointSet& other) const;
    PointSet& operator&(const PointSet& other);
    PointSet& operator=(const PointSet& other);
    bool isMember(const Point& point) const;
    Point* getPointsList();
private:
    Point* _allPoints;
    int _size;
    int _capacity;
};

and this is the problem code in my main:

for(int i = 0; i < listLen; ++i)
    {
        pointsList[i] = pointsList[i + 1];
    }

what will happen to the object in pointList[0]?? (pointsList is list of Point objects)

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
Aviad Bs
  • 1
  • 2
  • 1
    Since you are new to C++, don't just talk the problem. show code that describes it. BTW, it looks like you would like some [good C++ materials](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – WhiZTiM Sep 02 '17 at 12:02
  • 2
    (1) why use `new` (should be avoided if at all possible)? (2) store the object in the list not a pointer to the object. (3) use `std::unique_ptr` if you have to use pointers. (4) Use RAII to manage object life-times – Richard Critten Sep 02 '17 at 12:02
  • Use std::vector as the "list". –  Sep 02 '17 at 13:18
  • Read up on smart pointers and standard library containers (especially `std::vector`). – Jesper Juhl Sep 02 '17 at 17:45

1 Answers1

0

Unlike C# in c++ you only use the new operator when declaring raw pointers to objects eg.

PointSet *mypoint = new PointSet();

I wouldn't recommend using raw pointers at all if you can help it. Use shared_pointers instead as you don't have to worry about memory leaks. You can then replace new with make_shared.