Namespaces, Enums, Unions, and Struct

Hello!
I am currently in Data Structures and my teacher did not cover these data types. I am very curious and want to infuse my code to be better with the titles listing. I have read a bunch about each one of the data types and understand the basics but I would like to find a resource that will tell me how and when to use them. What I am saying is that I grasp the difference between them but not the significance of the difference. Also, I would like to find some projects that deal with these data types so I can put them into practice.
Thanks lol
If you are up for language feature examples that are not "hold my hand, I'm just a beginner" you could snoop around cppreference.

https://en.cppreference.com/w/
I appreciate it! How about projects that use a multitude of these data structures?
Write code, then some more code. Code using various "data structures" until your eyes and fingers bleed and your brain revolts against the abuse.

Beyond that your questions are rather vague to be able to give bite-sized and easily digestible answers.

You are taking the course, I don't have a clue what is being taught and what is being left out.
Forget about unions - they should not be used in modern C++.
std::variant is the C++ option.
Namespaces have nothing to do with data types.
Thank you for the suggestion and advice
I would say that you do not actually understand them. This is not meant to pick on you, just a warning -- reading about them won't drive home the weirdness of a union specifically, its just too unlike what a new programmer is used to seeing.

so real fast... a union's members share the same memory space. That is, if you had union of double and 64 bit int, and set the double to pi and the int to 1234, the double would stop being pi, because you changed the bytes of that location and both items share the same location. You can't do that in strict c++, but the example explains what the union is doing: its a bunch of bytes that can be viewed as different types: its a type that can hold ONE of its fields and which of those fields it is using is just casting the bytes back to the type.

in pointer terms, a union is working exactly like this:
char c[8];
double * d = (double*)c;
uint64_t *i = (uint64_t*)c; //see how both d and i are really just c? the 8 bytes of C are being used for 2 things, and that memory can only hold one 8 byte value at any one time, so....
*d = 3.14;
*i = 1234;
cout << *d; //not 3.14, it was modified when i changed.

structs have one copy of each of their fields. struct with a double and an int can store one double and one int at the same time: they group related data together into one type, like a string and its length, or a matrix of <complex> and its dimensions, etc. structs are identical to classes except for structs default members public, classes default them private, both can be over-ridden. c++ coders use structs mostly for method-less containers of data, and classes tend to have the fancy inheritance & methods and extra frills (this is just historic/convention, though, you can do both with either).

enums are a way to name constant integers as a group. eg red,green, blue as 0,1,2
so that instead of saying pixel[0] you can say pixel[red] which is easier to read. Because they are grouped, you can manage them a little more cleanly than 3 ungrouped constants and use them as a type. There is also an enum class, which adds a little more to this idea and has additional safety.

you should not use unions at all (as noted variant, pointer casting, or templates replace the things unions were created to do), enums when you need grouped constant integers, and classes/structs will be very common in your code as you get out of the beginner material.

in classic terms none of these are a data structure. They are the tools you could use to make a data structure.

Find a nice linked list homework example to see how a class/struct can be used to make a data structure. Enums ... I used to use them a lot with arrays before <vector> was considered superior (before it existed and in the early days it was poorly implemented on some compilers). you can do something like red,green,blue, emax and make an array char color[emax] ... if you later insert black after blue, and white after black, emax automatically updates and your array goes from 3 locations to 5 locations, a recompile and your new code just works... this can still be useful but less so in the vector era.
Last edited on
a union should be used when you want a different view of the same data. They really have no use in beginner projects, and are usually a C-style construct used in lower level code. I have used unions before when implementing certain emulators of video game systems. You will likely never encounter unions again in a beginner course.
C++ made that undefined behavior ^^^ you can only use a union to look at one field now, and morphing to take another view has to be done another way. Almost all compilers support what you said, but strict compiler flags will trigger on it.
Topic archived. No new replies allowed.