Array of objects, the difference between Java and C ++

I am new to C ++ and I am porting a Java project to C ++.

Consider the following Java code, where Piece is a class representing a chess piece:

Piece[][] myPieces = new Piece[8][8]; 

It creates an array where all entries have a null value.

How can I achieve the same in C ++? I tried:

 Piece* myPieces = new Piece[8][8]; 

But this will create an array with all the entries initialized by the default constructor.

thanks

Edit: I want the C ++ code to be efficient / elegant, and I don't care and don't need to copy the insert from Java to C ++. I am happy to greatly change the structure of the code, if necessary.

Edit 2: The code for the chess program, the size of the array will never change, and the performance is critical.

+6
source share
9 answers

The answer depends, because contrary to Java, in C ++ you have different property semantics and object lifecycle management (these two go hand in hand).

If you want to model objects similar to java, you should write:

 using PiecePtr = std::shared_ptr<Piece>; std::array<std::array<PiecePtr, 8>, 8> Pieces; 

shared_ptr has similar semantics with a java object (pass it wherever it is guaranteed, and if there are links to it).

If you want to model the observed objects (i.e. the array does not own them), you should write:

 using PiecePtr = Piece*; std::array<std::array<PiecePtr, 8>, 8> Pieces; 

This ensures that when the Pieces object is destroyed, the actual parts themselves remain in memory.

If you want to model unique objects belonging to the Pieces array, you should use:

 using PiecePtr = std::unique_ptr<Piece>; std::array<std::array<PiecePtr, 8>, 8> Pieces; 

This ensures that when the Pieces object is destroyed, the actual parts themselves will also be destroyed.

+3
source

The easiest way to declare an array of optional 8x8 objects in C ++ is as follows:

 boost::optional<Piece> myPieces[8][8]; 

The type boost::optional is an optional object (for example, your null references in Java) that does not have all the problems with using pointer types. It should be available as part of the standard library in the next few years.

You can use the std::array type, which is an encapsulation of fixed-size arrays, which allows them to be perceived as first-class citizens, and also provides a more convenient interface:

 std::array<std::array<boost::optional<Piece>, 8>, 8> myPieces; 

If you want to resize arrays at runtime, consider std::vector .

+7
source

How do you want it to be executed, and for C ++ instead of silent translation, how about this:

Use a POD type size 1 for a piece.
Add all the convenient methods you may need:

 struct Piece { unsigned char value; constexpr Piece() : value() {} constexpr operator bool() const {return !value;} constexpr bool empty() const {return *this;}; constexpr bool black() const {return value&0x80;} constexpr bool white() const {return value && !black();} constexpr unsigned piece() const {return value & 0x7f;} }; 

Now this will be the equivalent raw array:

 Piece board[8][8]; 

Or use std::array :

 #include <array> std::array<std::array<Piece, 8>, 8> board; 
+5
source

In C ++, you would do something like:

 std::vector<std::vector<std::unique_ptr<Pieces>>> myPieces; 
+2
source

Semantically equivalent:

 Piece* myPieces[8][8] 

since java only knows objects on the heap, pointers.

Since Piece is probably not the final class, but has a king, queen, this is the way to go.

+2
source

In C ++, the created object (even in an array) is created using the default constructor. This is one of the important differences from java. If you want to call the constructors separately, just use a vector of vectors and add each of them.

+1
source

I have no experience with java, but I believe that I got it could be a good replacement in C ++:

 std::array<std::array<unique_ptr<foo>, 8>, 8> arr = {}; if(arr[2][3].get() == nullptr) // Can check for null elements std::cout << "this is null"; arr[3][4].reset(new foo()); // Initialize an element 
  • Smart pointer avoids memory leaks.
  • std :: array provides performance comparable to a regular C array
  • aggregate initialization provides each pointer with a null value
  • fixed size as java array
+1
source

So you want to make a chess engine and performance is critical. There are several online guides for this. Speed ​​is important for chess AI, so it can take into account more moves per second, but for this you may have to sacrifice elegance.

You can either save the values ​​of the pieces in the array array directly, or save the fragments in a separate support array and create a board as pointers to these parts. There are some advantages to the second approach, which I cannot recall right now.

 std::array<std::array<Peice *, 8>, 8> Board; std::array<Piece, 32> Pieces; 

You can represent an empty cell as a null pointer.

If you want everything in one array, you can just use

 std::array<std::array<Peice, 8>, 8> Board; 

But you will need to create a "dummy" value to represent an empty cell.

Please note that there is no dynamic memory allocation and the data is compact in memory, therefore cache performance is improved.

A piece can be an enumeration or structure with some useful getter functions, such as IsWhite.

+1
source

In C ++, you should declare as:

 Piece *** myPieces; 

then highlight as:

 myPieces = new Piece **[8]; 

then

 for (int i = 0; i < 8; i++) { myPieces[i] = new Piece *; } 

Now, if you do,

 myPieces[0][0] = new Piece(); // C++, calls default constructor of Piece 

In Java,

 Piece[][] myPieces; myPieces = new Piece[8][8]; 

now if you do that

 myPieces[0][0] = new Piece(); // Java, calls default constructor of Piece 

Since you already have 8x8, you can also declare how (in C ++):

 Piece * myPieces[8][8]; // 64 pointers preallocated as 8 rows, 8 cols 

then

Now, if you do,

 for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { myPieces[i][j] = new Piece(); // or new Pawn or new Knight etc, subclass of Piece }} 

or highlight as necessary, for example.

 myPieces[0][0] = new Piece(); // or new Pawn or new Knight etc, subclass of Piece 
0
source

Source: https://habr.com/ru/post/975769/


All Articles