Beginner questions about structs!

Hi I am just looking into using structs, in the tutorial videos I have seen on youtube so far they seem to be declared outside of the main function so I am guessing the use for this (and their purpose) is basically to be global, available for all functions.

I am currently creating 5,6 vectors of the same type inside my main function and passing them to 2 or 3 different functions after they have been processed by one they get processed to the other etc. I have been looking at ways to clean up the aesthetics of the code for my own sake. I was considering using typedef to avoid writing std::vector<std::string> myVec or std::vector<int> myVec which I think is a good solution.

Somebody on this forum suggested I looked into using a struct (so I did!) , it would be much easier to declare them once, not have to "pass" them in the function names to each function and just be able to type structname.firststruct etc. So I have a couple of questions regarding this.

Is it fine to make a struct if its all made of the same variable type? is the above example making good use of a struct and what other useful features or basic implementation examples of using structs are there that you could explain to me?

Thanks for taking the time!
Last edited on
A struct is simply a grouping of data and methods (functions) to act on that data. In C++, generally people only stick data in structs. This is mostly a historical and stylistic thing. When people read 'struct' they're going to be expecting only data. Use a class if you want methods also.

Learning some OOP concepts would be very helpful for you here.
Hmm I just checked out a few tutorials around classes and it seems that most of them are based around the functions being similar in the class and being made public. Is this the best way to go? in my mind it would make more sense if the variables I have that are all of the same type being in the class, and each function just uses those public variables within the class?
Last edited on
Typically, you make variables private in a class, and have public methods that you allow to operate on them. This gives you control of what variables you want write access to, provides a layer of abstraction where you can stick sanity checks in, and provides looser coupling (should always strive for that in OOP).

Structs, since you usually are only sticking data in there it makes no sense to make that data private, so variables are public in struct (which is the default). Classes default to private access.
Yea that makes alot more sense, actually managed to find a decent tutorial on exactly this from Bucky (what a boss!)

http://www.youtube.com/watch?v=jTS7JTud1qQ

Thanks for the help RB
No problem. I wouldn't take Bucky as your only resource though :) Fine for starting out, but he definitely neglects lots of information.

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap-2010/lecture-notes/MIT6_088IAP10_lec04.pdf

Decent looking intro to OOP and how C++ uses it. MIT offers many courses as online resources, definitely something to look into.
Topic archived. No new replies allowed.