Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectors within classes: handling copy constructor and destructor (C++)

Take a simple class with the "big 3" (constructor, copy constructor, destructor):

#include <vector>
using namespace std; //actually goes in the C file that links to this header file
...
class planets(){ //stores mass and radii data for planets in a solar system.
   public:
      vector <double> mass;
      vector <double> radius;

   //constructor
   planets( int numObj ){
     for(int i=0; i<numObj; i++){
         mass.push_back(8.0); //some default values.
         radius.push_back(2.0);
     }
   }
   //copy constructor
   planets(const planets &p){
      vector <double> mass(p.mass); //copy vectors into new class.
      vector <double> radius(p.radius);
   }
  //destructor
  ~planets(){
     delete mass; //ERROR: (...) argument given to ‘delete’, expected pointer
     ~radius(); //also causes error: no match for call to(...) 
   }
}

I plan on making a vector of planets, thus the need for the "big 3":

vector <planets> stars;
stars.push_back(planets(5)); //5 hypothetical planets of alpha centauri
stars.push_back(planets(8)); //our solar system. Used to be nine.
///etc.

How do I delete the mass and radius vectors properly, to avoid memory leaks (do I even have to)?

like image 660
Kevin Kostlan Avatar asked Aug 18 '10 09:08

Kevin Kostlan


People also ask

What happens if you use the default copy constructor for vector?

The default copy constructor will copy all members – i.e. call their respective copy constructors. So yes, a std::vector (being nothing special as far as C++ is concerned) will be duly copied.

What is a copy constructor and destructor?

There is a concept of copy constructor which is used to initialize an object from another object. Syntax: ClassName() { //Constructor's Body } Destructor: Like a constructor, Destructor is also a member function of a class that has the same name as the class name preceded by a tilde(~) operator.


5 Answers

Your class can be simplified too:

class planets()
{ //stores mass and radii data for planets in a solar system.
  public:
    std::vector<double> mass;
    std::vector<double> radius;

  //constructor
  planets( int numObj )
  {
    for(int i=0; i<numObj; i++)
    {
      mass.push_back(8.0); //some default values.
      radius.push_back(2.0);
    }
  }
}

Your class does not contain any resources (ie pointers)
Therefore you do not need to explicitly manage them. Each class is supposed to know how to:

  • Copy Itself
  • Be assigned over
  • Destroy itselft

The compiler generated copy constructor assignment operator and destructor will automatically call these operations on any member variables (mass and radius) so you don't need too. So in your case the std::vector knows how to correctly do all three operations and therefore you do not need to add any extra code.

like image 141
Martin York Avatar answered Oct 04 '22 05:10

Martin York


No, you don't need to do anything because you aren't managing any resources. You only write the Big Three when you're managing a resource, but vector is doing that. It's the one with the Big Three properly written, you just use it.

This is why the single responsibility principle is key in resource management: once you have some class that properly manages a resource, you can simply use it without ever worrying about that resource again. Always split resource management from resource use.

The reason you need the Big Three written in a managing class is because the default special members typically do the wrong thing (they copy, assign, destruct values instead of what the values manage/point at.) But once you're resource is wrapped up (like in a std::vector), everything is just fine. The defaults will copy vector, but that copying is correctly written.

By the way, the Big Three is in the context of managing resources (copying and destroying resources), not created them. So it would be copy-constructor, copy-assignment, and destructor, not default constructor.


For your information, here's how you would do it:

class planets
{
public:
    // ...

    //copy constructor
    planets(const planets &p) : // use an initialization list to initialize
    mass(p.mass), // copy-construct mass with p.mass
    radius(p.radius) // copy-construct radius with p.radius
    {
        // what you had before just made a local variable, copy-constructed
        // it with p.xxx, then got released (nothing happened to your members)
    }

    //destructor
    ~planets()
    {
        // nothing to do, really, since vector destructs everything 
        // right for you, but you yes, you would delete any resources
        // you managed here
    }
};

But don't forget the copy-assignment operator. I recommend the copy-and-swap idiom, and leave that as an exercise for you.

(Remember you don't actually need these, though.)

like image 32
GManNickG Avatar answered Oct 04 '22 03:10

GManNickG


The 'big three' aren't what you say they are. They are: copy constructor, copy assignment operator and destructor.

vector instances are already copiable and assignable, you don't have to do anything special so with two members of type vector<double> you don't need to provide custom implementations of the big three.

Your copy constructor is incorrect, it doesn't copy the source vectors in to the new class, it just constructs function locals from them which are then discarded. This creates local variables called mass and radius which mask the member variables with the same name.

planets(const planets &p){
  vector <double> mass(p.mass); //copy vectors into new class.
  vector <double> radius(p.radius);
}

More correct (but unnecessary) would be:

planets(const planets &p)
  : mass(p.mass) //copy vectors into new class.
  , radius(p.radius)
{
}

Similarly your destructor body should be empty. You only delete pointers which have been allocated with new. As you have straight member variables no special action is required.

like image 36
CB Bailey Avatar answered Oct 04 '22 03:10

CB Bailey


You don't have to implement any destructor for your class. The vectors will be automatically destroyed. This is related to the Resource Acquisition Is Initialization pattern.

like image 31
Johan Kotlinski Avatar answered Oct 04 '22 03:10

Johan Kotlinski


No - you don't have to. member variable's destructors are automatically invoked after the containing object's destructor.

like image 26
Tony Delroy Avatar answered Oct 04 '22 04:10

Tony Delroy