2D array of objects without double pointer

I dont like using double asterisk. Its too ugly and unreadable
if I have


GameEntity* bricks[8][8];

How can I initiate it on the constructor?
Last edited on
> How can I initiate it on the constructor?
In other words, you want to allocate the array with new[]?
I think yes. Something like in c-sharp version

Cell[,] cells = new Cell[8, 8];

But my constructor contains parameter. So I have no idea how
Last edited on
Show me your constructor function.
Actually I dont have any from constructor because Im really confuse as how to initiate it..
closed account 5a8Ym39o6 wrote:
In other words, you want to allocate the array with new[]?


Don't use raw pointers, arrays and especially not new

@skadush
Use smart pointers, and/or STL containers with ordinary objects. The latter does memory management itself (on the heap), the former could be used for virtual polymorphism, and also uses the heap. Smart pointers use the concept of RAII, which means they behave well in terms of destruction, and can't leak memory. Smart pointers are more like the C# new - they are destroyed automatically, where as C++ new is not.

One can construct an object first, then put in in the container.


The functions std::make_unique and std::make_shared take arguments which effectively construct an object.

http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared

Good Luck !!
Last edited on
Thanks for the tip but still cant figure out how to initiate a custom class in a 2D array with parameters
Using raw pointers is not recommended if you are a professional. However, you may try :
1
2
3
4
5
6
7
8
9
// Create a 8x8x64 board
GameEntity* bricks[8][8];
for (int i = 0; i < 8; i++)
{ 
    for (int j = 0; j < 8; j++)
    {
        bricks[i][j] = new GameEntity[64];
    }
}
> How to initiate a custom class in a 2D array with parameters
Show us your GameEntity class.
This is the game entity class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class GameEntity
{
public:
	GameEntity(sf::Vector2f position, sf::Texture image,sf::Sprite sprite, sf::RenderWindow& window, float scale, sf::Vector2f velocity);

	~GameEntity();


	void Render(sf::RenderWindow& window);
	void Update();
	void GameEntity::Update(sf::Vector2f position);
	sf::Vector2f position;
	sf::Sprite sprite;
	sf::Texture image;
	sf::Vector2f size;
	float& scale;
	sf::RenderWindow& window;
	sf::Vector2f velocity;
	

	friend std::ostream& operator<<(std::ostream& os, sf::Sprite& sprite);

	friend std::ostream& operator<<(std::ostream& os, sf::RenderWindow& window);

};
How would you call this function (just an example) :
GameEntity(sf::Vector2f position, sf::Texture image,sf::Sprite sprite, sf::RenderWindow& window, float scale, sf::Vector2f velocity);
Here is an example you use smart pointers :
1
2
3
4
5
6
7
8
9
// Create a 8x8x64 board
std::unique_ptr<GameEntity> bricks[8][8];
for (int i = 0; i < 8; i++)
{ 
    for (int j = 0; j < 8; j++)
    {
        bricks[i][j] = std::unique_ptr<GameEntity> (new GameEntity[64]);
    }
}
Last edited on
im not really sure whats going on but why does your every reply got reported? Did you just report yourself?

Anyway this is how I call

player = new GameEntity(position,image,sprite,window,scale,sf::Vector2f(500,0));
Last edited on
> Why does your every reply got reported? Did you just report yourself?
Probably someone hates me so he did this.

> Anyway this is how I call...
Okay, you can use this :

1
2
3
4
5
6
7
8
9
10
11
// Create a 8x8x64 board
std::unique_ptr<GameEntity> bricks[8][8];
for (int i = 0; i < 8; i++)
{ 
    for (int j = 0; j < 8; j++)
    {
        bricks[i][j] = std::unique_ptr<GameEntity> (new GameEntity[64]);
        for (int k = 0; k < 64; k++)
        new (&bricks[i][j][k]) GameEntity(position,image,sprite,window,scale,sf::Vector2f(500,0));
    }
}


Tell me if the code compiles.
skadush wrote:
im not really sure whats going on but why does your every reply got reported?


Just thought I would let you know: closed account is a troll. Messages to admin have been sent by various people about this. Note that it may be more than one person doing the reporting. Be very wary of the advice that is offered by this user. For example: how many GameEntity objects are created by this last version of the code?

I am pretty sure there is documentation you can read about sfml, probably lots of examples.
@TheIdeasMan
It is my first time using std::unique_ptr. Tell me if the code compiles.
Topic archived. No new replies allowed.