can an object have a member that is of the same base class?

Jun 10, 2020 at 3:30pm
I'm making a snake game using different classes. I have a movingObject class that is the base class for everything that moves. I have a player class that inherits from it. The player is basically the head of the snake, then I have another class for the tail. In the player class I have a vector of tail segments, so it can grow. I have a function in my game class that detects collisions between moving objects. My tail works fine, grows and all that if I don't have it inherit from moving object, but in order to make it solid, in order to detect a collision, I need to make it inherit from movingObject. It complains a ton though whenever I try. Is there some rule that I can't have a member object of the same base class? I'll try to include an abbreviated version of what I've got to help you understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 Class MovingObject
{
Point point;
Velocity velocity;
int hitRadius;
bool alive;
virtual void draw();
virtual void advance();
etc.
};

Class Player: MovingObject
{
vector<Tail>tail;
void moveLeft();
void moveRight();
...
virtual void Draw();
etc.
};

Class Tail: MovingObject
{
virtual void draw();
virtual void advance();
etc.
};


obviously, not all of the code is here, just a few pieces to help you get the idea.
Jun 10, 2020 at 3:37pm
It complains a ton
is not a helpful description of the problem. Is there a reason why you thought the best way to get help would to be to withold the actual error messages from us?

Is there some rule that I can't have a member object of the same base class?

No, there is no such rule.
Topic archived. No new replies allowed.