2011-12-09 32 views
0

我被告知我必須爲我的Bullet類定義一個賦值操作符,但是我的印象是,只有當你真正需要實現三條規則時,纔會自己​​明確地處理內存,比如指針類成員。在這個代碼中調用賦值操作符在哪裏?

我被告知,試圖調用operator =的行在下面的代碼中是std::vector<Bullet> bullets。我只是不明白賦值運算符在哪裏被調用,以及爲什麼它被調用。我無處可做類似Bullet bullet1 = bullet2;

編輯 - 還有,爲什麼默認賦值運算符不適合?我在Bullet的層次結構中沒有任何指針成員。

感謝您的幫助。

#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 

這是同一個文件的源:

#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; 
} 

這是子彈:

#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 

遊戲對象:

#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 

回答

1

在源您有:

bullets = std::vector<Bullet>(); 

默認=操作者將不能工作,因爲GameObject具有不能被分配給一個基準部件mesh。 (它幾乎像一個指針,它擁有一個實際類別或結構的地址)

+0

並且你知道爲什麼默認賦值運算符不適合我的課嗎? – SirYakalot

+0

@SirYakalot:是的,我編輯了答案......這是因爲參考成員。 –

+0

所以它可能會寫一個重載的運算符=在GameObject中,甚至沒有子彈那麼高,對吧?因爲Bullet的默認操作符=會在GameObject中調用正確的重載?或者是那個錯誤...? – SirYakalot

2

GameObject(因此它的所有衍生物)都包含對Mesh的引用。引用在構造時被初始化,並且不能被重新分配,所以在這種情況下編譯器不會生成默認的賦值運算符。

1

首先,不產生默認operator=的原因是因爲 GameObject包含參考(Mesh& mesh)。對於包含一個或多個引用的類, 以及基類沒有operator=的類,編譯器不會生成operator=

在您發佈的代碼中沒有明顯的解決方案;你不能有一個 向量不可分配的對象。

問題是:你真的想能夠複製這些對象。 我只是從名字中猜出來的,但是這些聽起來像是對象,它們具有身份,或者在代碼完成後將具有身份。例如,如果您 更改了Bullet的位置,例如,您不希望讓 追蹤所有副本,並將其更改爲— a Bullet具有標識。

的解決方案是使用std::vector<Bullet*>,和分配所有的 的Bullet(和其他一切從GameObject派生) 動態。如果你考慮這個問題,這是正常的:你的物體具有由遊戲進化決定的生命週期,並且獨立於範圍或者主要是任何其他物體的生命週期都是 。 Bullet可能是一個例外,因爲如果Gun包含它們是 被破壞,那麼其中的Bullet也應該被銷燬。但是 我仍然以更通用的方式處理這個問題,破壞者 Gun負責銷燬其Bullet。更一般地說,當一個對象被銷燬時,你將不得不查找引用它的其他對象的所有 ,並刪除它們的指針。 通常情況下,這需要像觀察者模式—我 尚未看到任何智能指針正確處理這一點。

相關問題