Sign in to follow this  

Array of Classes static initialisation Java vs C++

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, I'm playing with some Java tetris code from Nathan Williams of MIT (http://www.mit.edu/people/nathanw/) and am trying to implement his Java classes in C++. The source can be found at http://www.mit.edu/people/nathanw/java/TetrisSrc.html On line 7 of TetrisPiece.java there is the member array pieces which is used to initialise instances of the Grid class with common Grid configurations ie tetris shapes. static Grid pieces[]= new Grid[7]; What I'm not sure of, is how to declare my own static array of Grid objects in C++. What I have done is break it up into the header which containts the class declaration: static Grid pieces[7]; and the cpp which has the code: Grid TetrisPieces::pieces[] = { Grid(3,3), Grid(2,2), Grid(3,3), Grid(3,3), Grid(3,3), Grid(3,3), Grid(3,4) }; This compiles fine but I can't initialise the grid ie a call to: Grid TetrisPieces::pieces[0].mGrid[0][0] = 0; will result in the compile error C2466: cannot allocate an array of constant size 0 Any ideas on the cleanest way to implement this Java code in C++? Cheers, Graham

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by GTowse
Any ideas on the cleanest way to implement this Java code in C++? Cheers,

http://www.gamedev.net/columns/hardcore/tetris/page2.asp

/compulsory

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could use a singleton-like class and initialize the pieces in its constructor.

Like so:

class Piece {
...
};

class Pieces {
private:
Piece* pieces;

Pieces() {
pieces = new Piece[7];
pieces[0]..mGrid[0][0] = 0;
...
}

~Pieces() {delete[] pieces; }

public:
static const Piece& getPiece(int i) {
static Pieces pieces;
return pieces.pieces[i];
}

};


And then instead of pieces[0] you'd do Pieces::getPiece(0).

Share this post


Link to post
Share on other sites
To the first anonymous post... hilarious ;) and to the second anonymous post, not bad, but I don't think a singleton class would be the best solution for initialising the grid, it seems a little overboard/not quite right, not to mention that the code you gave as an example does not cater for an array of objects, only one, as some grids have different sized arrays to others and this needs to be accomodated for in the solution. I should point out that once the grids are initialesed they are not changed, but instead used as templates to copy from, so a compile time assignment of values would be better than a runtime one, if that makes any sense. Any other takers?

Cheers,

Graham

Share this post


Link to post
Share on other sites
Sorry anonymous man, I took another look at that code and it would support multiple Grid objects, but alas, it will also include the data for every common Grid configuration in each instance of the Pieces class (child of Grid class), whereas the static Java method only passes the single instance needed for copying, I don't know if yours is an optimal technique...

[Edited by - GTowse on March 25, 2006 5:28:36 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by GTowse
Sorry anonymous man, I took another look at that code and it would support multiple Grid objects, but alas, it will also include the data for every common Grid configuration in each instance of the Pieces class (child of Grid class), whereas the static Java method only passes the single instance needed for copying, I don't know if yours is an optimal technique...
This pieces-class should be completely separate from the TetrisPiece and Grid classes. They would just use it. And you'd only have one instance of it. TetrisPiece converted from Java would simply be:

class TetrisPiece : public Grid {
public:
TetrisPiece(int i) : Grid(Pieces::getPiece(i)) {}
TetrisPiece() : Grid(Pieces::getPiece(rand() % 7)) {}
TetrisPiece(const Piece& p) : Grid(p) {}
};

Share this post


Link to post
Share on other sites
Ah thanks, yeah that would definately work. I will likely go that way, I guess there's just nothing in C++ that can be used similarly to the way that Java code operates. Thanks for your help,

Sincerely,

Graham

Share this post


Link to post
Share on other sites

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this