【问题标题】:Inheriting a constructor in a .h and .cpp file在 .h 和 .cpp 文件中继承构造函数
【发布时间】:2011-10-17 21:50:01
【问题描述】:

所以我有两个类:Object 和 Player。 我希望 Player 从 Object 继承,因为 Object 具有我需要的基本功能。 Player 具有扩展 Object 可以执行的附加功能。 我有四个文件: Object.cpp、Object.h、Player.cpp 和 Player.h。

为了举例说明我的情况,我在我的 Player 类中添加了一个变量: 播放器变量。我的 Object 构造函数参数不包含这个,但是我的 Player 构造函数包含,所以你可以看到 Player 扩展了 Object。

无论如何,这是我的代码:

对象.h:

#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>

class Object{
int x, y;
HTEXTURE tex;
hgeAnimation *anim;
float angle, FPS, velocity;

public:
    Object(int x, int y, HTEXTURE tex, float FPS);
    //Setters
    void SetX(int x);
    void SetY(int y);
    void SetSpeed(int FPS); //Image Speed
    void SetAngle(float angle); //Image Angle
    void SetVelocity(float velocity); //Object Speed/Velocity
    //Getters
};

对象.cpp:

/*
** Object
** 
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"

Object::Object(int x, int y, HTEXTURE tex, float FPS){
    this->x = x;
    this->y = y;
    this->tex = tex;
    this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
    this->x = x;
}

void Object::SetY(int y){
    this->x = x;
}

void Object::SetSpeed(int FPS){
    this->FPS = FPS;
    anim->SetSpeed(FPS);
}

void Object::SetAngle(float angle){
    this->angle = angle;
}

void Object::SetVelocity(float velocity){
    this->velocity = velocity;
}

//Getters

播放器.h:

#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"

class Player : public Object{
    int playerVariable;

public:
    Player(int x, int y, HTEXTURE tex, float FPS, int playerVariable);
    //Setters
    void SetX(int x);
    void SetY(int y);
    void SetSpeed(int FPS); //Image Speed
    void SetAngle(float angle); //Image Angle
    void SetVelocity(float velocity); //Object Speed/Velocity
    //Getters
};

播放器.cpp:

/*
** Object
** 
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
#include "Player.h"

Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable){
    this->x = x;
    this->y = y;
    this->tex = tex;
    this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
    this->x = x;
}

void Object::SetY(int y){
    this->x = x;
}

void Object::SetSpeed(int FPS){
    this->FPS = FPS;
    anim->SetSpeed(FPS);
}

void Object::SetAngle(float angle){
    this->angle = angle;
}

void Object::SetVelocity(float velocity){
    this->velocity = velocity;
}

//Getters

我的问题是,我不确定我是否设置正确。 我看过关于继承的教程,但我不知道如何设置它 类的 .h 文件和 .cpp 文件。有人可以帮忙吗?

【问题讨论】:

  • 陈述您的实际问题,并发布一些(最少的)代码来说明您的问题。
  • .cpp 中的所有内容都应仅引用 Player。您只需要在播放器的声明中提及基类

标签: c++ inheritance constructor


【解决方案1】:

您定义了两次Object 功能。您不需要定义它,它将继承自 Object 并自动在 Player 上可用。

您的Player 构造函数需要调用基础Object 构造函数。这是通过构造函数初始化列表完成的:

Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
   : Object(x, y, tex, FPS) // forward arguments to base constructor
{}

【讨论】:

    【解决方案2】:

    您可以通过这种方式调用基类构造函数,指定每个参数:

    Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
    

    【讨论】:

    【解决方案3】:

    看起来您实际上不需要 Player 类来覆盖 ObjectSetX(int)SetY(int)SetSpeed(int)SetAngle(float)SetVelocity(float)。如果你这样做了,那么你将把这些Object 成员函数设为virtual

    其他几点说明:

    1. 您不需要在Player.cpp 中重新定义Object 成员函数。 Player是一个Object,所以它继承了类Object的成员函数。此外,如果 Object.cppPlayer.cpp 中的实现之间存在任何差异,那么由于违反单一定义规则,您将收到链接器错误。
    2. Object 析构函数应声明为virtual
    3. 您可以使用初始化列表,而不是直接在Player 构造函数中设置Object 的成员:

      Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
          : Object(x, y, tex, FPS), playerVariable(playerVariable)
      {
      }
      

      使用初始化列表效率更高。在这种情况下甚至是必要的,因为 Object::xObject::yObject::texObject::FPS 都是 Object 和朋友的私有。

    4. Object 中,您应该提供自定义复制构造函数和复制分配运算符重载(Object::operator=(const Object&amp;))。 C++ 编译器为您提供了这些成员函数的默认实现,但由于您有指针成员和HTEXTURE 成员,您可能需要提供显式实现来处理深拷贝。复制构造函数和复制分配重载必须始终进行深层复制。
    5. FPS 的类型不匹配。 Object::FPS 成员被声明为float,但Object::SetSpeed(int) 的参数是int

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 2016-03-24
      • 2011-12-27
      • 2017-11-17
      • 2020-08-07
      相关资源
      最近更新 更多