[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
What exactly is the point of classes over structs? Is there
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 17
Thread images: 1
File: 1463683265485.jpg (318 KB, 634x875) Image search: [Google]
1463683265485.jpg
318 KB, 634x875
What exactly is the point of classes over structs? Is there a tangible benefit to locking in data-method relationships like that?
>>
>>54732399
Welcome to the bright side
>>
A struct is a set of related data, while a class is a set of related data and functions to handle it.
>>
>>54732527
Why do the functions need to be strictly combined with the data? Why not just have them separate in the same header?
>>
>>54732542
cuz muh OOP
>>
>>54732554
Well that's what my question is. What's the point of classes? What is the strict benefit gained from strictly combining the methods to the data? It seems to me that that just creates the possibility for huge convoluted messes
>>
>>54732399
sure typeclasses are based
oh you mean OOP? That is trash stay clear
use Functional programming though
>>
>>54732542
Because objects can have inheritance, interfaces, abstraction, encapsulation, etc.
>>54732562
It certainly does, there are plenty of examples of it in many OOP projects. The advantages stem from how powerful objects are as a concept. You can have an abstract object that has all the basic data and functionality and then have a bunch of object types built off of that that implement a certain function a different way, or have a different default set of data in them, or construct differently, etc.
The power of OOP doesn't come from the way it's organized, it comes from inheritance and interfaces.
>>
>>54732562
Is everyone here so young they really have no idea why you'd want to tie data and methods together? It's called encapsulation and it's so you can treat a class as a black box. As long as the interface of the class doesn't change you can switch the entire implementation by changing a single line.
>>
>>54732611
can't you inherit and interface manually through inclusion of proper header files?

frankly I've never understood the point of interfaces, but I've only ever done personal projects and never anything on a team
>>
>>54732562
It's like global functions that aren't really global. You don't have to pass class's fields to its methods in order to use them. With properties you can do actions on reading/modifying fields. You can also override operators for classes. The only benefits I can think of.
>>
>>54732631
Oh and inheritance/interfaces allows to write functions accepting different types without manual overloading. In sepples its even possible to use duck typing via templates.
>>
>>54732626
Not in the same way as with classes, headers aren't the same concept. Headers are "I want to use these functions and this data from this file," classes are "I want to have this encapsulated set of data that I can generate infinite instances of and extend to add more specific functionality."

Interfaces are used if you want to have a group of related classes with the same function names but different functions.
Like you could have an ISorter interface that then has a bunch of different objects implementing it that all use different sorting algorithms, allowing you to now interchangeably pass them with no extra code.
>>
>>54732697
I see

Thank you! I am working on a simulation game right now where you are a commodities trader and I was doing this thing that frankly seems like a bit of a mess with structs but I think I may as well just use classes
>>
Structs: fields are public by default, preferably allocated on stack, should be easily serializable.
Classes: fields are private by default, preferably allocated on heap.
You can do all the OOP shit with both of them, differences are purely semantical nowadays.
>>
>>54732717
>You can do all the OOP shit with both of them,
Can you do inheritance with structs now?
>>
>>54732738
Yes.
Thread replies: 17
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.