#1 Members - Reputation: 283
Posted 13 April 2012 - 09:28 PM
#2 Crossbones+ - Reputation: 5172
Posted 13 April 2012 - 10:42 PM
Make something, think about where you could use structures and group data together, and then make another game and improve upon your previous design. This is how it is done. This is what we all have done.
One less-intuitive way in which structures are useful is when you want to make a function but you have a feeling the number of parameters are going to grow in the future as you add more functionality. Instead of constantly adding new parameters to the function, just make it accept a structure and add new members to the structure.
L. Spiro
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#3 Members - Reputation: 1302
Posted 13 April 2012 - 10:45 PM
In short: whenever something belongs together (and will be used more than once), put it in a struct.
I guess this needs some example to demonstrate how utterly messy and unmanagable things would be without structs.
Start simple, you want to manage people. Let's say they have a name, age and gender. Let's also use a simple array instead of lists to store them. Also, have a function that can print a persion.
Without structs
string name[50];
int age[50];
int gender[50];
void printPerson(string name, int age, int gender)
{
cout << name << age << gender;
}
or even some really bad style, that requires all your above arrays to be (semi)-global
void printPerson(int index)
{
///
cout << name[index] << age[index] << gender[index];
}
With structs:
struct Person
{
string name;
int age;
int gender;
};
Person people[50];
//I deliberately ignore the option to pass by reference
void printPerson(Person someone)
{
cout << someone.name << someone.age << someone.gender;
}
//There is now no good reason to pass an index to the function.
Let's say you need a function to create a person or read one from a file
Without struct
void readPerson(File file, string* name, int* age, int* gender)
{
*name = ...;
*age = ...;
*gender = ...;
}
using it would require this:
string name;
int age;
int gender;
readPerson(file, &name, &age, &gender);
If you had a struct, it could be easier
Person readPerson(File file)
{
Person person;
person.name = ...;
person.age = ...;
person.gender = ...;
return person;
}
and using it would just be
Person person = readPerson(file);
Now let's add Addresses and Shops without using a struct for the address.
struct Person
{
string name;
string street;
string zip;
string city;
};
//Yes, this is easy to write AT FIRST by just copy/pasting stuff... and you will hate yourself for every single Ctrl+V when you need to add the country to the address EVERYWHERE
struct Shop
{
Person owner;
string name;
string street;
string zip;
string city;
};
compare that to
struct Person
{
string name;
Address address;
};
struct Shop
{
Person owner;
string name;
Address address;
};
In short, structs let you structure your code and avoid redundancy,usually resulting in lots of copy/paste. If you find yourself copy/pasting whole blocks of code, stop it and ask yourself why you even NEED to do that in the first place.
Structured code means less work, easy to maintain, extend and use. Also a lot less bugs, which is what happens when code becomes a huge mess where it gets increasingly hard to figure out what is happening when, where and how.
#5 Senior Moderators - Reputation: 3113
Posted 14 April 2012 - 12:17 AM
In C++ there is no difference between struct and class except in the default visibility of members and default inheritance visibility.What is the advantage of using a struct over say making a Person or Shop class that has the same information? Why would you use one and not the other?
Specifically:
Struct's by default have public members and public inheritance.
Class's by default have private members and private inheritance.
Other than that both are EXACTLY IDENTICAL.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX
#6 Members - Reputation: 155
Posted 14 April 2012 - 01:09 AM
// Nvm, just read Washu's comment. So basically if I am writing my own code for my own use, theres not much difference whether I use structs or classes.
#7 Members - Reputation: 1302
Posted 14 April 2012 - 01:58 AM
#8 GDNet+ - Reputation: 189
Posted 14 April 2012 - 06:04 AM
As a rule of thumb structures are used for sets of data that you want public. Structs are best used when you want to pass a set of data to a function like in Trienco's example above or you want a class to utilize a set of data but you want it grouped together.
struct ScreenDimensions {
int screenWidth;
int screenHeight;
};
class GraphicsDevice {
public:
GraphicsDevice( ....ScreenDimensions dimensions .....);
private:
ScreenDimensions m_screenDimensions;
};
GraphicsDevice::GraphicsDevice(...ScreenDimension dimensions...) :
m_screenDimensions( dimensions ) {
...
}






