Please don't use a manually allocated array. Use vector instead.
std::vector<Cell> vec(rows*cols); and then access the element you want with [x*cols + y]. Or std::vector< std::vector<Cell> > vec(rows, cols); will also work, and give you [x][y] indexing.
You should not be using new and delete manually for something so trivial. You have no need of polymorphism here, and the ownership semantics are absolutely clear.
There is no dependency problem here, if you simply forward declare one or the other or both. C++ is not so fragile as you seem to think. You would only have trouble if you tried to define the member functions inline where they interacted with the other class.
Even if it did, it's terribly ugly and it would be unacceptable if you were being paid for it.
This is what I mean when I say you shouldn't try and code C++ like it's Java. IT'S NOT.
std::vector<Cell> vec(rows*cols); and then access the element you want with [x*cols + y]. Or std::vector< std::vector<Cell> > vec(rows, cols); will also work, and give you [x][y] indexing.
You should not be using new and delete manually for something so trivial. You have no need of polymorphism here, and the ownership semantics are absolutely clear.
KonVex said:There should be dependency problems! Here is how I would declare the classes:
class Cell
{
Board *OwnerBoard;
// attributes
public:
Cell(Board *);
// methods
}
class Board
{
Cell *BoardData;
// attributes
public:
// methods
}
There is no dependency problem here, if you simply forward declare one or the other or both. C++ is not so fragile as you seem to think. You would only have trouble if you tried to define the member functions inline where they interacted with the other class.
GOOD. C++ doesn't enforce the ability to dynamically cast from any given type to any other given type, and that is if anything a good thing from a safety perspective. You can easily do it yourself, however, if you really want all your classes to descend from the same one, no one is stopping you.KonVex said:Cell can`t know about Board because it wasn`t declared yet!
If I swap the declarations the problem will just be shifted.
Also C++ doesn`t have a generic Object class like JAVA.
KonVex said:I could change Cell`s constructor to
Cell(void *pBoard); (and change Board *OwnerBoard to void *)
and use a typecast (e.g. (Board *)pBoard) after Board is known, but that wouldn`t work in all instances!
Even if it did, it's terribly ugly and it would be unacceptable if you were being paid for it.
This is what I mean when I say you shouldn't try and code C++ like it's Java. IT'S NOT.