![](/img/trans.png)
[英]Global const initializing and difference between constructor in .h or .cpp file
[英]Inheriting a constructor in a .h and .cpp file
所以我有两个类:Object和Player。 我希望Player从Object继承,因为Object具有我需要的基本功能。 Player具有它的附加功能,这些功能从Object的功能开始扩展。 我有四个文件:Object.cpp,Object.h,Player.cpp和Player.h。
为了举例说明,我在Player类中添加了一个变量:playerVariable。 我的Object构造函数参数不包含此参数,但是我的Player构造函数包含该参数,因此您可以看到Player扩展了Object。
无论如何,这是我的代码:
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
};
Object.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
Player.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
};
Player.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文件。 有人可以帮忙吗?
您要两次定义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
{}
您可以以这种方式调用基类构造函数,并指定每个参数:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
它看起来像你实际上并不需要一流的Player
覆盖SetX(int)
SetY(int)
SetSpeed(int)
SetAngle(float)
,并SetVelocity(float)
的Object
。 如果这样做,则可以将这些Object
成员函数virtual
。
其他一些注意事项:
Player.cpp
重新定义Object
成员函数。 Player
是一个Object
,因此它继承了Object
类的成员函数。 另外,如果Object.cpp
和Player.cpp
的实现之间存在任何差异,则由于违反“一个定义规则”,您将收到链接器错误。 Object
析构函数声明为virtual
。 您可以使用初始化列表来Object
直接在Player
构造函数中设置Object
的成员:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable) : Object(x, y, tex, FPS), playerVariable(playerVariable) { }
使用初始化列表效率更高。 在这种情况下甚至有必要,因为Object::x
, Object::y
, Object::tex
和Object::FPS
都是Object
和其朋友专用的。
Object
,您应该提供一个自定义的复制构造函数和copy-assign运算符重载( Object::operator=(const Object&)
)。 C ++编译器为您提供了这些成员函数的默认实现,但是由于您具有指针成员和HTEXTURE
成员,因此可能需要提供显式实现来处理深层副本。 复制构造函数和复制分配重载必须始终进行深复制。 FPS
的类型不匹配。 Object::FPS
成员被声明为float
,但是Object::SetSpeed(int)
的参数是int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.