简体   繁体   English

在.h和.cpp文件中继承构造函数

[英]Inheriting a constructor in a .h and .cpp file

So I've got two classes: Object and Player. 所以我有两个类:Object和Player。 I want Player to inherit from Object because Object has basic functions that I need. 我希望Player从Object继承,因为Object具有我需要的基本功能。 Player has it's added-on functions that expand from what Object can do. Player具有它的附加功能,这些功能从Object的功能开始扩展。 I have four files: Object.cpp, Object.h, Player.cpp, and Player.h. 我有四个文件:Object.cpp,Object.h,Player.cpp和Player.h。

To make an example out of my situation, I have added a variable to my Player class: playerVariable. 为了举例说明,我在Player类中添加了一个变量:playerVariable。 My Object constructor parameters does not contain this, however my Player constructor does, so you can see that Player expands Object. 我的Object构造函数参数不包含此参数,但是我的Player构造函数包含该参数,因此您可以看到Player扩展了Object。

Anyways, Here is my code: 无论如何,这是我的代码:

Object.h: 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.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: 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: 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

My problem is, I'm not exactly sure if I'm setting this up right. 我的问题是,我不确定是否要正确设置此设置。 I've looked at tutorials on inheritance but I have no idea how to set it up with both a .h file and a .cpp file for the classes. 我看过有关继承的教程,但是我不知道如何为类设置.h文件和.cpp文件。 Can someone help? 有人可以帮忙吗?

You are defining the Object functionality twice. 您要两次定义Object功能。 You don't need to define that, it will be inherited from Object and automatically be available on Player . 您无需定义它,它将从Object继承并自动在Player上可用。

Your Player constructor needs to invoke the base Object constructor. 您的Player构造函数需要调用基础Object构造函数。 This is done with constructor initialization lists: 这是通过构造函数初始化列表完成的:

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) {

It looks like you don't actually need class Player to override SetX(int) , SetY(int) , SetSpeed(int) , SetAngle(float) , and SetVelocity(float) of Object . 它看起来像你实际上并不需要一流的Player覆盖SetX(int) SetY(int) SetSpeed(int) SetAngle(float) ,并SetVelocity(float)Object If you did, then you would make these Object member functions virtual . 如果这样做,则可以将这些Object成员函数virtual

A few other notes: 其他一些注意事项:

  1. You don't need to redefine the Object member functions in Player.cpp . 您无需在Player.cpp重新定义Object成员函数。 Player is an Object , so it inherits the member functions of class Object . Player是一个Object ,因此它继承了Object类的成员函数。 Also, if there are any differences between the implementation in Object.cpp and Player.cpp then you will get linker errors due to a violation of the One Definition Rule. 另外,如果Object.cppPlayer.cpp的实现之间存在任何差异,则由于违反“一个定义规则”,您将收到链接器错误。
  2. The Object destructor should be declared virtual . 应该将Object析构函数声明为virtual
  3. Rather than setting the members of Object directly in the Player constructor, you could use an initialization list: 您可以使用初始化列表来Object直接在Player构造函数中设置Object的成员:

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

    Using initialization lists is much more efficient. 使用初始化列表效率更高。 It is even necessary in this case because Object::x , Object::y , Object::tex , and Object::FPS are all private to Object and friends. 在这种情况下甚至有必要,因为Object::xObject::yObject::texObject::FPS都是Object和其朋友专用的。

  4. In Object , you should provide a custom copy constructor and copy-assign operator overload ( Object::operator=(const Object&) ). Object ,您应该提供一个自定义的复制构造函数和copy-assign运算符重载( Object::operator=(const Object&) )。 The C++ compiler provides default implementations of these member functions for you, but because you have pointer members and HTEXTURE members, you likely need to provide explicit implementations to handle the deep copy. C ++编译器为您提供了这些成员函数的默认实现,但是由于您具有指针成员和HTEXTURE成员,因此可能需要提供显式实现来处理深层副本。 The copy constructor and copy-assign overload must always make deep copies. 复制构造函数和复制分配重载必须始终进行深复制。
  5. There is a mismatch on the type of FPS . FPS的类型不匹配。 The Object::FPS member is declared as a float , but the parameter to Object::SetSpeed(int) is an int . Object::FPS成员被声明为float ,但是Object::SetSpeed(int)的参数是int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM