dimanche 19 septembre 2021

How do I create a class with reference member and vector of pointers?

I am fairly new to C++ and am working on a personal project. I want to create a vector<Entity*> entitities in C++ where each Entity object is unique. I have a class inside header Entity.h that I want to create. Now Entity takes two member variables:

  1. Rectangle rect - an Object of type Rectangle that has four float variables as member variables (x1, y1, x2, y2) which are the coordinates of two opposite corners of a rectangle
  2. vector<Component*> - a number of different components of Base class type Component with some Derived classes. The code for Class Component looks like so:
/*************************** BASE CLASS **************************/
class Component
{
public:
    virtual ~Component() = default;
    virtual Component* Clone() = 0;
};

/*************************** DERIVED CLASS 1 **************************/
class BasicComponent: public Component
{
private:
    int B= 0;
public:
    BasicComponent* Clone() override {
        return new BasicComponent(B);
    }

    BasicComponent(const int& mB) :B(mB){}
    HealthComponent() :B(0) {}

};

/*************************** DERIVED CLASS 2 **************************/
class AdvancedComponent: public Component
{
private:
    float A = 0.f;
    int iA = 0;
public:
    AdvancedComponent* Clone() override {
        return new AdvancedComponent(A, iA);
    }

    AdvancedComponent(const float& mA, const int& miA) :A(mA),iA(miA) {}
    MovementComponent() :A(0.f),iA(0) {}

};

Since I want each Entity in the vector of entities to be unique, that is, have it's own rectangle and components, how should I create the class ?

My question here is, what should the class Entity look like ? Should I create separate CopyConstructor, Assignment Constructor and Destructor for this class ? Also if I want to implement copying one Entity into another (deep copying), is it necessary to have all 3 (Copy, Assignment and Destructor) ?

Aucun commentaire:

Enregistrer un commentaire