I am told I have to define an assignment operator for my Bullet class, however I was under the impression that the only time you really needed to implement the rule of three was when you explicitly handle memory yourself, such as a pointer class member.
I am actually told that the line that is attempting to invoke the operator= is std::vector<Bullet> bullets in the following code. I just don’t understand where the assignment operator is being called, and why it’s being called. Nowhere am I doing something like Bullet bullet1 = bullet2;
EDIT – and also, why isn’t the default assignment operator suitable? I have no pointer members anywhere in Bullet’s hierarchy.
Thanks for the help.
#ifndef GUN_H
#define GUN_H
#include "gameObject.h"
#include "Bullet.h"
#include <vector>
class Mesh;
class Gun : public GameObject
{
private:
std::vector<Bullet> bullets; // this line right here
protected:
virtual void TriggerPulled() = 0;
public:
Gun(Mesh& mesh);
virtual ~Gun();
std::vector<Bullet>& Bullets();
};
#endif
this is the source for the same file:
#include "Gun.h"
#include "Mesh.h"
Gun::Gun(Mesh& mesh) : GameObject(mesh)
{
bullets = std::vector<Bullet>();
}
Gun::~Gun() {}
std::vector<Bullet>& Gun::Bullets()
{
return bullets;
}
this is bullet:
#ifndef BULLET_H
#define BULLET_H
#include "BoundingSphere.h"
#include "helperMethods.h"
#include "GameObject.h"
class Bullet : public GameObject
{
private:
float velocity;
float distance;
D3DXVECTOR3 firedFrom;
BoundingSphere bSphere;
public:
Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR colour, Mesh& mesh);
~Bullet();
BoundingSphere& BulletSphere();
};
#endif
BoundingSphere:
#ifndef BOUNDING_SPHERE_H
#define BOUNDING_SPHERE_H
#include <d3d10.h>
#include <d3dx10.h>
class Ray;
class BoundingBox;
class BoundingSphere
{
private:
D3DXVECTOR3 centre;
float radius;
public:
BoundingSphere(D3DXVECTOR3 position, float radius);
BoundingSphere();
bool Intersects(BoundingSphere boundingSphere);
bool Intersects(BoundingBox boundingBox);
bool Intersects(Ray ray, D3DXVECTOR3& result);
// getters
const D3DXVECTOR3 Centre();
const float Radius();
// setters
void BoundingSphere::Centre(D3DXVECTOR3 centre);
};
#endif
gameObject:
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
#include <d3d10.h>
#include <d3dx10.h>
class Mesh;
class GameObject
{
private:
D3DXVECTOR3 scale;
D3DXVECTOR3 rotation;
D3DXVECTOR3 position;
D3DXCOLOR colour;
D3DXMATRIX matFinal;
Mesh& mesh;
protected:
void Draw(D3DMATRIX matView, D3DMATRIX matProjection);
public:
GameObject(Mesh& mesh);
virtual ~GameObject();
//getters
const D3DXVECTOR3 Scale();
const D3DXVECTOR3 Rotation();
const D3DXVECTOR3 Position();
const D3DXCOLOR Colour();
//setters
void Scale(D3DXVECTOR3 scale);
void Rotation(D3DXVECTOR3 rotation);
void Position(D3DXVECTOR3 position);
void Colour(D3DXCOLOR colour);
};
#endif
In the source you have:
The default = operator won’t work because
GameObjecthas a reference membermeshwhich can’t be assigned to. (It’s almost like a pointer. It holds an address to an actual class or structure)