Jump to content

  • Log In with Google      Sign In   
  • Create Account

Importance of structs?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 stein102   Members   -  Reputation: 475

Like
0Likes
Like

Posted 13 April 2012 - 09:28 PM

Right now I'm reading Accelerated C++ and I just finished a section that discusses structs. My question is, what practical use are they for game development? They look really useful, I'm just not sure what they would be used for. Thank you.

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13577

Like
0Likes
Like

Posted 13 April 2012 - 10:42 PM

Structures are useful for organizing related sets of data together. To understand how this is useful in games you simply need experience in making games.
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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
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
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 Trienco   Crossbones+   -  Reputation: 2172

Like
2Likes
Like

Posted 13 April 2012 - 10:45 PM

You put data in structs. Especially if you need that data "as a whole". Imagine you have a bunch of things that can have an address (persons, shops, ...). You do NOT want to spam street/zip code/city/country as separate members into every single one of them. Most likely you will have functions that operate on addresses (printing them on labels, doing a sanity check). And again you wouldn't want each of these functions to take each value as a separate parameter. Even more so you may have a function that should create and RETURN an address. Functions simply can't return more than a single thing (unless you go with ugly workarounds like having half a dozen output parameters, which are an ugly concept that should be avoided whenever possible).

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.
f@dzhttp://festini.device-zero.de

#4 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 13 April 2012 - 11:46 PM

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?

#5 Washu   Senior Moderators   -  Reputation: 5180

Like
1Likes
Like

Posted 14 April 2012 - 12:17 AM

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?

In C++ there is no difference between struct and class except in the default visibility of members and default inheritance visibility.

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 HNikolas   Members   -  Reputation: 192

Like
0Likes
Like

Posted 14 April 2012 - 01:09 AM

Can anyone point me out the strong sides classes have over structures in C++ besides data hiding?

// 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.
Ruby on Rails, ASP.NET MVC, jQuery and everything else web.. now also trying my hand on games.

#7 Trienco   Crossbones+   -  Reputation: 2172

Like
0Likes
Like

Posted 14 April 2012 - 01:58 AM

While technically there is no real difference, you might sometimes find code where structs are used for plain data and classes are used when it also contains functions to operate on that data. So if you do want to make a difference, make it the distinction between data vs. behavior. Or in other words, if your thingy is supposed to "do" something, call it a class, if it's merely a dumb container for data, call it struct.
f@dzhttp://festini.device-zero.de

#8 bglanzer   Members   -  Reputation: 459

Like
0Likes
Like

Posted 14 April 2012 - 06:04 AM

like stated before structs are exactly the same as classes except by default thier member variables are public instead of private like in a class. A structure can utilize custom constructors, destructors, methods, and operation overiding.

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 ) {
...
}

Brendon Glanzer





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS