sdasdadas
sdasdadas

Reputation: 25096

How do I store objects within another object in C++?

I want to make sure that I'm creating/destroying this object properly...

This is the definition of my Camera object which contains references to Vector objects:

#ifndef CAMERA_H
#define CAMERA_H

#include "vector.h"

class Camera {
 private:
  Vector* location;
  Vector* direction;
  float velocity;
 public:
  Camera();
  ~Camera();
};

#endif

which are created in the constructor:

#include "camera.h"

Camera::Camera() {
  location = new Vector(1.0, 1.0, 1.0);
  direction = new Vector(1.0, 1.0, 1.0);
  velocity = 0.0;
}

Camera::~Camera() {
  delete location;
  delete direction;
}

then, whenever I want a camera object I simply call Camera camera.

Upvotes: 4

Views: 153

Answers (3)

billz
billz

Reputation: 45410

Am I correct in assuming that when the variable camera goes out of scope, the destructor will be called and I won't suffer any memory leak?

yes

If I want to remove the variable camera before the scope closes, is it correct to perform delete camera?

No, camera is not allocated by new operator, you can not delete it, just leave it until it goes out of scope. Unless call new/delete to force object duration.

Potential memory leak:

In below code, there is a chance to leak memory. if constructs location finishes but direction = new Vector(1.0, 1.0, 1.0); fails and exception is thrown, Camera destructor won't be called thus location memory is leaked.

Camera::Camera() {
  location = new Vector(1.0, 1.0, 1.0);
  direction = new Vector(1.0, 1.0, 1.0);
  velocity = 0.0;
}

A better solution: There is no need to introduce pointer for Vector members. Use automatic storage should be preferred.

class Camera {
 private:
  Vector location;
  Vector direction;
  float velocity;

 public:
  Camera() 
  : location(1.0, 1.0, 1.0), 
    direction(1.0, 1.0, 1.0),
    velocity(0.0f)
  {
  }
};

Upvotes: 2

Alok Save
Alok Save

Reputation: 206526

Yes and Yes(provided Camera was created by calling new).
But you also need to follow the Rule of Three.

Also, it is a good idea to rethink if you really need dynamically allocated members?
remember that by using raw pointers with dynamic memory you are taking responsibility of explicitly doing the memory management for your class which is easy to go wrong with.You are much better off simply using instances rather than pointers:

  Vector location;
  Vector direction;

If you must use pointers atleast use smart pointers instead of raw pointer members.

Upvotes: 2

WhozCraig
WhozCraig

Reputation: 66194

To answer your immediate question, Yes, the destructor will be fired, and yes, you're memory will clean up. But you have not provided protection for incorrect copying, assigning of this object type, and virtual destruction, which can lead to significant problems down the road. More details on this can be found by reading up on The Rule of Three and many, many posts on the subject on SO.

Unless you need dynamic allocation (and you very likely do not) do it like this instead:

class Camera 
{
private:
    Vector location;
    Vector direction;
    float velocity;
public:
    Camera();
};

Camera::Camera() 
    : location(1.0, 1.0, 1.0)
    , direction(1.0, 1.0, 1.0)
    , velocity(0.0)
{
}

No destructor required.

Upvotes: 1

Related Questions