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:
Rectangle rect- an Object of typeRectanglethat has four float variables as member variables (x1, y1, x2, y2) which are the coordinates of two opposite corners of a rectanglevector<Component*>- a number of different components of Base class typeComponentwith some Derived classes. The code forClass Componentlooks 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){}
BasicComponent() :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) {}
AdvancedComponent() :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) ?
Entity, right? What’s stopping you from writing the class?vector<Entity*>relevant? Is it not enough to say that eachEntitymust have its own copy of theRectangleandComponents from which it was constructed?vector<Entity*>instead ofvector<Entity>?" -- That's not what I am getting at. You are asking about your code; I am talking about your question. Simpler questions have fewer distractions, hence are more likely to get answered without getting lost on a tangent. What is lost if you drop all mention of vectors from your question and just state that eachEntitymust have its ownRectangleandComponents? (Emphasizing the vector over the requirement makes it seem like maybe objects not in the vector must share rectangles and components.)