divine-code divine-code - 11 months ago 48
C++ Question

C++: Can a class has an object of it's own type?

I am trying to solve the Conway's game of life in c++. So according to my design, I have a cell class which contains a list of 8 neighbours, which itself is an object of type cell. Is it ok to do that. The code looks like this

class Cell {
int position_row_;
int position_colm_;
std::shared_ptr<State> state_;

std::vector<Cell> neighbours_;

Now, the other question that is bugging me is, what type of relationship is it. While designing i thought of it to be 'Aggregation'; but now i don't think so.
Can someone please clarify?

Answer Source
std::vector<Cell> neighbours;

You are storing copy of neighbours. What if state of any neighbour changes? Do you need that change to be reflected into the vector? If yes, better store the pointers to the neighbours:

std::vector<std::shared_ptr<Cell>> neighbours;