Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of objects, difference between Java and C++

Tags:

java

c++

arrays

I am new to C++ and I am porting over 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 the entries are null.

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

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

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

Thanks

Edit: I want the C++ code to be efficient/elegant and I do not care nor wnant to copy paste from Java to C++. I am happy to heavily modify the code structure if needed.

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

like image 598
Romain Avatar asked Sep 24 '14 11:09

Romain


People also ask

Are arrays in Java the same as C?

Arrays. Arrays in Java are very close to their C parent (to the point that we even have a method for efficient array-to-array-copy support using a bare-bone native implementation: System. arraycopy()). They represent contiguous memory spaces.

What is the difference between array and array of objects in Java?

Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.

Can Java have array of objects?

Answer: Yes. Java can have an array of objects just like how it can have an array of primitive types. Q #2) What is an Array of Objects in Java? Answer: In Java, an array is a dynamically created object that can have elements that are primitive data types or objects.


2 Answers

The simplest way to declare an 8x8 array of optional objects in C++ is like so:

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

The boost::optional type represents an optional object (like your nullable references in Java) that doesn't have all the pitfalls of using pointer types. It should be available as part of the standard library in the next few years.

You may prefer to use the std::array type, which is an encapsulation of fixed-size arrays that allows them to be treated as first-class citizens and also provides a nicer interface:

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

If you want to be able to resize your arrays at run-time, consider std::vector instead.

like image 122
Joseph Mansfield Avatar answered Oct 04 '22 09:10

Joseph Mansfield


As you want it performant, and right for C++ instead of a dumb translation, how about this:

Use a size-1 POD-type for piece.
Add all the convenience-methods you might want to it:

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 that would be an equivalent raw array:

Piece board[8][8];

Or use std::array:

#include <array>
std::array<std::array<Piece, 8>, 8> board;
like image 26
Deduplicator Avatar answered Oct 04 '22 10:10

Deduplicator