为bool分配值

时间:2015-07-28 19:51:15

标签: c++

好的,提前预测。这可能是一个愚蠢的问题。

所以有一个简单的类,例如

class Bullets : public RenderEvent
{
public:
    /**
     * Constructor
     */
    Bullets();

    Bullets(Vector3D position, Vector3D direction);
    /**
     * Destructor
     */
                        ~Bullets();
    /**
     * Sets up the model and sets the bullets id
     */
    void                CreateBullet();
    /**
     * Renders the bullet if it is active
     */
    virtual void        Render();
    /**
     * Updates the movement of the bullet and detects if it has hit any characters
     * if it is still active
     */
    void                Update(
                            float elapsedTime
                            );
    /**
     * Sets the position of the bullet
     */ 
    void                SetPosition(
                            const Vector3D& pos
                            );
    /**
     * Gets current position of the bullet
     */
    const Vector3D&     GetPosition() const;
    /**
     * Sets the current rotation of the bullet
     */
    void                SetRotation(
                            const Vector3D& rot
                            );
    /**
     * Gets the current rotation of the bullet
     */
    const Vector3D&     GetRotation() const;
    /**
     * Sets the direction the bullet should move
     */
    void                SetForward(
                            const Vector3D& forward
                            );
    /**
     * Gets the direction of the bullet
     */
    const Vector3D&     GetForward() const;
    /**
     * Sets the bullet active
     */
    void                SetAlive(
                            bool alive
                            );
    /**
     * Returns if the bullet is currently active
     */
    bool                GetAlive() const;

private:

    MyString m_ID; 
  Model* m_model;

    Vector3D m_position;
    Vector3D m_rotation;
    Vector3D m_forward;
    float m_lifePeriod;

    bool m_alive;


    };

    #endif //_BULLET_H_

bullet.cpp

const float BULLETROTATIONTIME = 2.f;
const float BULLETSPEED = 30.0f;
const float LIFETIME = 1.0f;
const MyString BULLETMODELPATH = "Assets\\Models\\HARPOON_BOLT_01.x";


/************************************************************************/

Bullets::Bullets()
{
    m_position = Vector3D(0,2,0);
    m_rotation = Vector3D(0,0,0);
    m_forward = Vector3D(0,0,1);
    m_lifePeriod = LIFETIME;
    m_alive = true;
    m_flags = NoEffects;

  CreateBullet();
}

Bullets::Bullets(Vector3D position, Vector3D direction)
{
  m_position = position;
  m_rotation = Vector3D(0,0,0);
  m_forward = direction;
  m_lifePeriod = LIFETIME;
  m_alive = true;
  m_flags = NoEffects;

  CreateBullet();
}

/************************************************************************/

Bullets::~Bullets()
{
    RenderManager::GetInstance()->RemoveEvent(this);

  delete m_model;
}

/************************************************************************/

void
Bullets::CreateBullet()
{
    m_model = new Model(BULLETMODELPATH, m_position, m_rotation, 2.0f);

    RenderManager::GetInstance()->AddEvent(this, NULL);
  m_alive = true;
}

/************************************************************************/

void
Bullets::Render()
{
  m_alive = true;
    if(m_alive)
    {
        m_model->Render();
    }
}

/************************************************************************/

void
Bullets::Update(float elapsedTime)
{
    if(m_alive)
    {

    }

}

/************************************************************************/

void
Bullets::SetPosition(const Vector3D& pos)
{
    m_model->SetPosition(pos);
}

/************************************************************************/

const Vector3D&
Bullets::GetPosition() const
{
    return m_model->GetPosition();
}

/************************************************************************/

void
Bullets::SetRotation(const Vector3D& rot)
{
    m_model->SetRotation(rot);
}

/************************************************************************/

const Vector3D&
Bullets::GetRotation() const
{
    return m_model->GetRotation();
}

/************************************************************************/

void
Bullets::SetForward(const Vector3D& forward)
{
    m_forward = forward;
}

/************************************************************************/

const Vector3D&
Bullets::GetForward() const
{
    return m_forward;
}

/************************************************************************/

void
Bullets::SetAlive(bool alive)
{
    m_alive = alive;
}

/************************************************************************/

bool
Bullets::GetAlive() const
{
    return m_alive;
}

/************************************************************************/

好的,我的问题是,当调用构造函数或在任何其他函数中时,我无法更改m_alive的值...它总是评估为false < / p>

我似乎无法在任何函数中更改类中任何位置的bool m_alive的值。

我不是初学程序员,但我为什么不能改变一个简单的bool的价值感觉很蠢....

什么可能干扰?

更新: 所以我确实修复了它(感谢@ user007)清理和重建修复它。但为什么?如果有人能解释为什么我不能改变变量的值,直到我清理过&#39;该项目?

此外,既然已经找到了答案,那么如果这个问题没有得到那么多的话,我会很高兴......(并且它没有被搁置)

1 个答案:

答案 0 :(得分:4)

从它的外观来看,你可能正在使用&#34;错误&#34;来构建你的Bullet课程。构造函数,绕过m_alive的赋值。

有几种方法可以解决它:

Bullet::Bullet() :
    m_alive(true)
{
}

Bullet::Bullet(Vector3 startingPosition, Vector3 direction) :
    Bullet() // calls default constructor (sets m_alive to true, always!)
{
}

或者,你可以在类定义中设置默认值(假设你的C ++版本支持这个):

private:
    bool m_alive = true;