When should I use struct?

Hello everyone! Can someone give me an example of why and when I should use a data structure? I've written this just to get the hang of it but I mean classes can do all of the things structures can and even more.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

//Data Structure
struct animal {
	string type, name, color;
	bool tail, wings;
} dog, cat, bat;

//Prototypes
void printAnimalInfo (animal animalType);

int main()
{
	//Set dog object members attributes
	dog.type = "Dog";
	dog.name = "Rosco";
	dog.color = "Black and White";
	dog.tail = true;
	dog.wings = false;

	//Print dog info
	printAnimalInfo (dog);

	cout << "\n";
	return 0;//(Ctrl + f5) to pause
}

void printAnimalInfo (animal animalType) {
	cout << "Animal type : " << animalType.type;
	cout << "\nAnimal name : " << animalType.name;
	cout << "\nAnimal color : " << animalType.color;
	cout << "\nAnimal has tail : " << (animalType.tail != 0 ? "True" : "False");
	cout << "\nAnimal has wings : " << (animalType.wings != 0 ? "True" : "False");
}


Thank you.
The only difference between struct and class is that struct have public members and use public inheritance by default while class use private by default.

I often use the struct keyword when I have all members public and no member functions but I could just as well have used the class keyword. There is no real difference so it's just a matter of taste.
Last edited on
Thank you for informing me with this information! I very much appreciate it :)
I just use struct or class depending on whether I want an implicit public: or private: at the beginning of the class definition so I can be a lazy coder.

struct, class, and union can all have public, private, and protected member variables and member functions (optionally virtual or pure virtual), public/private/protected constructors, and public/private/protected multiple inheritance.

Peter explained the public-private difference between class and struct, so I'll just add that unions have the default-public behavior too.
Last edited on
L B, unions can't be used with inheritance for some reason.
I use struct as a convention when I simply want an aggregate data type. If I intend for the data members to be operated on from outside then I use a struct, but if the data needs to be hidden and modified through an interface I will use a class.
As has already been said, structs are simply identical to classes except they default to public.


One useful use of this trait is a functor, such as the following:

1
2
3
4
5
6
7
struct GameObjectDeallocator
   {
     void operator()(const std::pair<std::string,VisibleGameObject*> & p) const
     {
       delete p.second;
     }
   };



A functor is simply an object that can be used like a normal function. Like so:

 
std::for_each(_gameObjects.begin(),_gameObjects.end(),GameObjectDeallocator());
I wish the operator() function could be static...just imagine,
ConvertTo<int>::(std::string("100"));
Currently you have to have a static function with a name like f
Well, Boost's lexical_cast uses the following notation (assuming namespace is being used)

int year = lexical_cast<int>("2001");

Do you need a :: ??
That's a function, and unfortunately it does not suit my needs of treating chars as numbers instead of characters. I have to achieve this with function overloading inside a templated class.
Last edited on
Topic archived. No new replies allowed.