Public Group

# structures question

This topic is 4055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

just read about structures so i guess i get the general idea of them but i was wondering how are they used in lets say a program? are they more for databases? are they used in video games and if so how? i made a basic program using structures that was just a copy of the tutorials since i have no other idea how to try to use them to get used to them and have it memorized. it was just a mock phone adress book or something *edit* also just o make sure you have structures and you need to define a variable to acess it right? and thats the thing you do before the .?
#include <iostream>
#include <string>
using namespace std;

main()
{
struct phonebook {
string name;
int number;
};

phonebook friends;
friends.number = 123456;
friends.name = "George Jeremy";

cout<< friends.name;
cin.get();
cin.get();
cout<< friends.number;
cin.get();

}



##### Share on other sites
In C++ a struct is the equivalent of a class except that by default, struct members have public scope. For example, the following two are equivalent:

struct MyStruct{     int x;};class MyClass{public:     int x;};

##### Share on other sites
There is little difference between structs and classes in C++. I tend to use them for entities that i don't consider active. For example, a Renderer would be a class because it does stuff, whereas a vertex would be a struct because it doesn't. It just holds data and has accessors.

Dave

##### Share on other sites
Quote:
 Original post by HeyHoHeyjust read about structures so i guess i get the general idea of them but i was wondering how are they used in lets say a program? are they more for databases? are they used in video games and if so how?

They are sort of designed for databases, yes, but databases crop up more often than you might think. A game, for example, might have a database of all the enemies that exist in the game world (or in a more general way, a database of all the game objects, including both enemies and other things like pickups or the player themselves). This database might only be implemented using a linked-list of structures, but it's still technically a database (WordNet definition: "an organized body of related information").

They provide a way of grouping related pieces of data together into one blob. This is useful both for the database scenario, and also for situations where you want to pass a bunch of things but only have enough room for one data value - you pass as your one data value a pointer to an instance of the structure. The Win32 API does this quite often.

Quote:
 also just o make sure you have structures and you need to define a variable to acess it right? and thats the thing you do before the .?
Not quite. A structure definition itself is like a blueprint; when you declare a variable using that structure, you're creating a new variable using that blueprint. So, you never 'access the structure' itself - you always work with a variable created using the structure (an 'instance of' that structure). Once you've declared the variable, then yes, you use the name of that variable before the . operator to access the fields of that instance.

##### Share on other sites
ok thank you everyone

##### Share on other sites
Structures aren't particularly "for" anything, in that they're not better suited for one field over another. Both databases and video games use structures.

Any time you want to group together several variables that have a common point (such as the name and number, as used in your phonebook example), you could use a structure. It also makes it simpler to pass to a function — instead of having to define a function that takes ten variables, you simply define it to take a structure. Of course, this isn't an end all to savings. It doesn't make a lot of sense to put variables that have no common purpose together in a structure.

And yes, you need to declare a variable of the structure before you can use it, which is the name you use before the dot operator.

Something that video games might use a structure for is character information.
struct Character{    string Name;    int Level;    int Exp;    int ExpToLevel;    int Str;    int Dex;    int Wis;    vector< Object& > Inventory;    Room& CurrentRoom;};

This is just pseudo-code (well, not attached to any particular project), but it should give you some ideas of ways you can use structures.

##### Share on other sites
ty tychon for that example. with that and everyone elses posts i actually have an idea of practical uses for it now.

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631355
• Total Posts
2999521
×