• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Help me with my c++ project?

Status
Not open for further replies.

maharg

idspispopd
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.

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.

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.
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:
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.
 

KonVex

Member
Thanks! You just answered most of my questions about OOP in one single post :)
I really didn't know that you could forward-declare classes (but I assume that all dependent classes should be declared in the same header).

I'm not really a Java guy, but more familiar with Turbo Pascal and assembler. To be honest I tried to ignore OOP as long as possible and now I have to play catch-up :/
 

Pochacco

asking dangerous questions
Turbo Pascal?
Wow. Those were the days.

OOP is wonderful - I love it.
At times, it's often more work to design something with Object-Oriented Principles, but it pays off in the long run if you ever have to go back change/update things.

That's why, for the original problem, even if the Cell object seems pretty useless (assuming you're gonna put all the logic into the board class), it's still worth modeling a cell as a class of its own - if only to be consistent with OOP principles.
 

Lathentar

Looking for Pants
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
}

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.

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!

I was mistaken. The cell SHOULDN'T know about the board.

Here is what you can do:

Have a function run through all the cells, telling each of them how many neighbors it has.
Then go through all the cells again and have them live or die based on how many neighbors it had during that stage.

That way you don't have to make a copy of the 2D array (or be all tricky) and the cell doesn't need to know about the board or its location.
 

Ferrio

Banned
Or std::vector< std::vector<Cell> > vec(rows, cols); will also work, and give you [x][y] indexing.

Oh, never knew you could index vectors like 2d arrays. Well that makes things a whole lot simpler. THough I'm curious what the greater than sign's purpose in that statement is.
 

Lathentar

Looking for Pants
Ferrio said:
Oh, never knew you could index vectors like 2d arrays. Well that makes things a whole lot simpler. THough I'm curious what the greater than sign's purpose in that statement is.

What are you talking about? What greater than sign? The template argument?
 
Status
Not open for further replies.
Top Bottom