Jump to content
  • Advertisement
Sign in to follow this  
BCullis

Array of structs: is the following possible?

This topic is 3738 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, So as per the traditional "learn by making progressively more detailed games" path, I'm working on a tetris clone in C++. The way I wanted to work the collision and display (SDL) depended on the idea that I could create the active game board as an array of structs that held two members: an int for a color enumerator and a bool for whether the cell was occupied or not. However, before I progress any further I want to make sure I can even do that. Here's some example code:
struct sCell
{
	int color;
	bool isFull;
};

void clearBoard(sCell[][] board)
{
	for(int i=0, i < 15, i++)
	{
		for(int j=0, j<10, j++)
		{
			board[j].isFull = false;
		}
	}
}
I realize there could be plenty of discussion over HOW I'm implementing this, but my question right now focuses on if this is even possible to do. Do I need to initialize each cell in the array as if they were classes (an interative "new" for each [j] when I create the actual board) or will it be enough to just say sCell[15][10] gameBoard; and be able to access/change the .isFull and .color members as I need to?

Share this post


Link to post
Share on other sites
Advertisement
Yes, you should be fine.

You seem to be confused about classes though. You don't need to use "new" with classes, you can use them as auto variables. In fact - in general you want to be using new as little as possible. You can dynamically allocate structures too.

Classes and structs are very similar in C++. The differences are:

* members in a struct default to public
* members in classes default to private
* inheritance in structs defaults to public
* inheritance in classes defaults to private

Share this post


Link to post
Share on other sites
No you can do it just like that. You can do the same for classes as well, there's really no difference between classes and structs in C++ (this is C++, right?) aside from members defaulting to public in structs and private in classes.

Share this post


Link to post
Share on other sites
You can have an array of structs. You can have an array of classes, too. The array can be any data type you want.

As far as your code, sCell shouldn't have the brackets. A struct itself isn't an array, it's a data type. Whatever you declare a struct can be an array, that's fine.

You mean you wanted to make the board an array, and be declared as type sCell so it can hold the int and bool values like this, right?


struct sCell
{
int color;
bool isFull;
};

void clearBoard(sCell board[][]) //The board is an array of type sCell
{
for(int i=0, i < 15, i++)
{
for(int j=0, j<10, j++)
{
board[j].isFull = false;
}
}
}


Share this post


Link to post
Share on other sites
Awesome, thanks guys.

rip-off, the public default is why I went with struct :)

Although, on this aspect:
Quote:

You seem to be confused about classes though. You don't need to use "new" with classes, you can use them as auto variables. In fact - in general you want to be using new as little as possible. You can dynamically allocate structures too.


I was under the impression that you can define a class, but each instance of that class needs its own "new" when it's created. For example (ignoring public/private for now):

class Foo
{
int a, b, c
Foo()
}

Foo::Foo(int x,int y,int z)
{
a=x;
b=y;
c=z;
}

Foo Foo1 = new Foo(1,2,3);
Foo Foo2 = new Foo(4,5,6);




Or am I confusing C# and C++ syntax?

Share this post


Link to post
Share on other sites
Quote:
Original post by Electrons
You can have an array of structs. You can have an array of classes, too. The array can be any data type you want.

As far as your code, sCell shouldn't have the brackets. A struct itself isn't an array, it's a data type. Whatever you declare a struct can be an array, that's fine.

You mean you wanted to make the board an array, and be declared as type sCell so it can hold the int and bool values like this, right?

*** Source Snippet Removed ***


Ah, yes, thank you, now I know why intellisense has been giving me problems as well :D

Share this post


Link to post
Share on other sites
Quote:

Or am I confusing C# and C++ syntax?


You are [smile]

In C++, you can use the following (I believe they are identical, the compiler is allowed to omit the copy construction that appears to occur):

Foo one = Foo(1,2,3);
Foo two = Foo(4,5,6);

Foo one(1,2,3);
Foo two(4,5,6);

Share this post


Link to post
Share on other sites
Quote:
Original post by BCullis

Or am I confusing C# and C++ syntax?


Yes indeed. You can have a variable whose type is a class or struct, it will be allocated on the stack and have its default constructor run at the point where the function where it's declared is run (and its destructor will be run when it goes out of scope, which is when that function exits). You can try something like this as an exercise:


#include <iostream>

class SomeClass
{
public:
SomeClass()
{
std::cout<<"Booga!"<<std::endl;
}

~SomeClass()
{
std::cout<<"Booga booga!"<<std::endl;
}
};

void DoStuff()
{
SomeClass something;
}


int main()
{
DoStuff();
return 0;
}


Share this post


Link to post
Share on other sites
Okay, one last question then, (thanks a bunch guys, cleared up my roadblock in a matter of minutes). With the syntactical correction Electrons gave me, when I go to declare the real gameboard array, which is the correct process:

sCell[10][15] gameBoard;

or

sCell gameBoard[10][15];

?

(I'm hoping it's the former, as I'd like to be able to pass the argument "&gameBoard" to all the functions that will operate on it (line clears, board clear, tetris piece member functions, etc)

Share this post


Link to post
Share on other sites
Quote:
Original post by BCullis
sCell gameBoard[10][15];


This one.

Quote:

(I'm hoping it's the former, as I'd like to be able to pass the argument "&gameBoard" to all the functions that will operate on it (line clears, board clear, tetris piece member functions, etc)


You can't use '&' or '*' on an array when passing to a function because an array is never passed by value, only by reference. If you don't want the array to be modified, put const in front of it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!