Every time you want to keep organized data, probably of heterogeneous types, grouped logically as one thing alone is useful. There are cases that even a given can be useful to do as well as an abstraction.
When you declare a struct
is just mapping how this data is set up in memory and you will be able to access each individual data through the names of its members. It only exists in the code, during execution there is nothing to indicate that it is a struct
, only has data, one after the other.
If you think this is similar to object orientation, it’s because you have no idea what object orientation is. But don’t worry, almost everyone programs like this, or thinks they program and also doesn’t know what it is. Gathering data into a single structure has never been object oriented, but actually many people think it is. And yet it uses object notation (noun, point, adjective), there it confuses even more.
In fact when it creates a struct
is creating a model of how an object will be composed, as well as a int
is an object composed of bits in a certain way that represents an integer and has a single member accessed directly to facilitate, ie object has always existed at all and people think object is something connected to a class, No, this is a universal concept independent of paradigm. I already answered about it in In programming, what is an object?.
The rest I’ve answered in How a "struct" is organized in memory?.
Struct and class are essentially the same thing by just changing the standard accessibility level of members. Struct is public, class is private.
– Kevin Kouketsu
IS that? I suggest you see here also, there is a lot about the subject...
– hkotsubo
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero