Difference between using struct and class to create an object

Asked

Viewed 516 times

2

He had already "learned" object orientation in college, right on top of it. Now that I’ve started to revise I realize that no matter how much they say structs do not form objects, I really see no difference between creating an object using a constructor and a struct using typedef.

What’s the difference between that:

//construtor em java
class objeto{
    String nome;
}
//...
novo_objeto = new objeto();

and this?

//struct em C
typedef struct
{
   char nome[30];
}objeto;
//...
objeto novo_objeto;

1 answer

6


There are some wrong questions and assumptions there. And I will disregard the wrong Java syntax and everything is out of pattern.

You cannot make an assessment of the differences just by syntax, and in such simple cases. Let’s say that had no difference in this case, would have in more complex cases.

What you wrote in Java is not a builder, It’s just a very simple class that has a lot of implications that you have no idea about and I don’t think it’s worth talking about in detail here.

You are not seeing but there is an access control to the data and the field in Java cannot be accessed by every application written thus, only in the current package, by default a data is restricted to the package. In C the data is always accessible by every application.

The most important difference is that a Java class can only be allocated in heap, whereas a struct in C is allocated by default in stack, can allocate in heap if necessary, the consumer has control of it. If you want to understand more of the subject you already have question here: What are and where are the "stack" and "heap"?.

It’s up to another question, so I won’t go into more detail, but in Java this class has only one pointer and one other object String separate will be allocated elsewhere. In C the form you used is making the string be allocated to the structure itself.

In Java the data is initialized properly, in C it is not, the code has to do it. It is not that this is strictly OOP, but it is different. Note that even if you haven’t explicitly created a constructor in the code you can only create the object by calling one that the compiler created for you. In C you don’t create an object, in fact what you did there was just reserve memory for the object, nothing else.

Object orientation

Actually this object orientation thing is much more complex than that, and to think that a class is OO is a mistake. If start reading on the subject (There is a lot to read, it is not enough to read one thing or another) you will see that you are far from understanding what is OOP and maybe you learned wrong from the beginning (at least that is what indicates the question). No problem, almost everyone learns and does wrong all their lives, OOP is harder than people think, and just because they find it easy tend not to learn right, there’s a good opportunity for you to get right.

In no way what is doing in Java can be considered a OO code, it is just a very simple data structure that creates a type (the default of Java is always create a type, no need to typedef, nor does C++ need) to happen to be encapsulated in a class, that’s all.

Object orientation is about the semantics you want to give to the object and what it does. You read this code and don’t know anything about it, don’t serve anything useful, don’t know what this object is about. This is important for OOP. The most you can see there is a class mechanism being used very simply.

One of the biggest mistakes that people make in learning OOP is using artificial examples, it only teaches wrong because it talks about the mechanism, which is not important. OOP serves to have the complexity of the problem better managed, which can only be done with a real concrete example.

The example is so bad that you can’t show other differences, if you had a method in it then you could already show something, if you had visibility specification, maybe inheritance, polymorphism, these other things would show differences because these things start to show visibly object orientation. If you had something more you could show something like this in Java:

novo_objeto.fazAlgo();

And in C:

fazAlgo(novo_objeto);

I put in the Github for future reference.

It seems silly but it is important because the object is highlighted and facilitates something in Java. This is just OOP syntax, it’s a visible difference, but it’s not how you do OOP. It even has language that allows this syntax even though nothing is OO.

Roughly OO is putting everything that belongs to the object together, is promoting reuse, trying to reproduce the real world object in the code. I don’t agree much with these statements they always make, but it’s a step in the right direction, even if you don’t hit the target is close, at the moment you’re going in the opposite direction thinking that simply the class says what OOP is.

Completion

Forget everything you know about OOP, because you said you do not know, look for a good book on the subject to learn step by step what is this paradigm (and try to read what Linkei and other sources, because many will teach wrong, since the author learned wrong, because there is a lot of wrong material out there, is a wireless phone without end), there is a lot of good stuff there on the subject, I do not say that is the best way to learn, But it gives you information that leads you the right way to understand that OOP is not just the way to use a very simple data structure. And do not forget to see other more important concepts even before OOP.

  • okay, thank you very much.

  • I do not understand why of the negative in the answer, the answer seems to me that answers the differences and still speaks of something important, people think they know OOP, but most have learned wrong and it is important to explain that this should be learned correctly. It’s not just plain use.

  • 1

    @Perhaps because I wrote running :) I got better. Or because one thinks that OOP is the class. Or because I said something about OOP that the person didn’t like, even though I wasn’t wrong. Or because I didn’t explain what is right, even though the question isn’t about it. It can always be a vote of someone who doesn’t like me, but I don’t think it was even the case. If she commented would help a lot and we could know and I could improve the answer, I have no commitment to error, maybe this person has. I can fix what’s wrong and we all win.

  • It was not I who had denied it, but I confess that, for me, the answer before it was edited had the defect of focusing more on the errors of the question than on the concept of OOP. But after editing, the answer has the Maniero quality seal and is, like many others, impeccable. + 1.

  • 1

    @v.Santos Note that I’ve changed the content and focus a lot, I’ve just highlighted this focus and made some things clearer. I don’t even think it’s impeccable, I didn’t think it was the best one, I think I can do better, but not so much because I don’t think the question helps much :) I realized that it was positive and so it couldn’t have been negative. Then I did a larger analysis and it looks like it was negative for tantrums with a recent case that happened. ;)

  • @Maniero, the first answer, except the point I pointed out earlier, was good. And I honestly admire your patience in writing (and rewriting) well-crafted answers even when the question doesn’t help much. As for "tantrums for a recent case", if you are referring to the question about the amount of languages C :) - I intend to insist to death that C++ and C# have this name because of their creators' lack of courage in using the name D - but I think you shouldn’t worry too much about it since I understood and agreed in part with your criticism and I hold no grudge :).

  • @v.Saints I get tantrums almost daily :D Some people like my style, others do not, never tried to please everyone, I know who I want to please and the attitude of the person defines it, luckily please these people ;)

  • I didn’t deny it either, I really liked the answer too

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.