Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLaurie Gray
23,119 PointsIs a struct kind of like an Object in this example?
I have some javascript experience and am trying to grasp this. Is the struct here like an object from js?
2 Answers
Andrei Fecioru
15,059 PointsStructs are a C++ construct. In C++, the only difference between structs and classes is that the members in structs are publicly visible by default (while in classes they are private by default).
That being said, most of the times structs are used as pure data containers. If you want to put a bunch of data together (with no methods which act on them) you may want to use a struct.
When you want to add some methods that act upon the data and you also want to have some internal representation that pertains to the said data (you may also want to hide the internal representation from the rest of your system) you will most likely want to use a class definition.
NOTE: you are allowed to define methods inside a struct in C++, but it is not a widely used practice due to the default public member access.
Laurie Gray
23,119 PointsThanks Andrei, Im struggling with this language but its going to be worth it!