Struct or Classes, huh?

Asked

Viewed 896 times

2

I made the code using structure, I wanted to know if with classes it would be more efficient. I’m doing it in C++ Builder. The idea of the code and the following: create a list of problems, where during the execution of the code will be added and removed numerous problems (of the type struct or class)

struct {
 TList *cidadesvisitadas; // lista iniciada com uma cidade ,vou acrescentando ate 30 cidades
 float  distancia;
 TList *cidadescanditadas; // lista iniciada com 29 cidades , que no decorrer  do codigo vai sendo retirada uma a uma e adicionada na lista de cidades visitadas
};

In the list of problems, an initial problem will be added (unresolved), and then removed this problem turns into three new problems (unresolved) that are added again to the list of problems, then remove a problem again from the list of problems and turns into three other problems and so on. The list at the end of the run is empty (add and remove, when the problem is solved it does not return to the list). Problems already dynamically staggered (there will be millions of problems) if I change the struct (Problem) by class (Problem), the code is more correct?

  • Take a look at [tour], you can vote on everything on the site you think is good content, and accept an answer in your questions, as you already know how to do.

2 answers

4


In C++ structures and classes are virtually the same (classes have private members by default), so it makes no difference, neither performance.

How it will assemble and use the code makes a difference. It is common for people to leave struct for simple structures without methods, i.e., POD (Plain Old Data), and classes when they are more complete objects. There are those who prefer to use struct when the type will be used more as value and class for objects that will usually be used as reference, but not always this works so black in white.

Of course classes cannot be used with C. Depending on how to use struct also can not.

-3

First of all, struct is not the same thing as a class as many answered you, although both are extremely similar, both in terms of syntax (written in the code itself) and in the displayed result.

  • Class: A class is a structure floodgate attributes and methods. To access these characteristics, it is necessary to create an object. You can access the information of a class after the creation of the object through the nomenclature objeto->atributo; or objeto->funcao();

Observing: One class can communicate with another just as one object can communicate with another.

  • Struct: A struct is a structure, as well as a class, but which attributes only. There is no way to create methods. Also, to access the characteristics of a structure is used a access mechanism, which is completely different from an object. To access the information of a struct you can use the nomenclature acesso.atributo;

Observing: A struct cannot communicate directly with another struct as well as an access mechanism cannot communicate with another.

In summary, a class is an extremely elaborate structure, with direct interactions and communications with the functionality of your program. A struct is only an aesthetic structure, which serves to couple similar data in only one place, although it is also functional to be able to create several access mechanisms and thus access several attributes.

Your question is not the difference between the two, but through this information you can deduce, when developing your project, which is the best option. From what I could understand, class would be a better option, since you will be able to elaborate the solution of the problems displayed through methods induced in the class itself.

  • 2

    This is simply false. struct is a class with public members by default. That’s it. In C, structs really only have attributes, but we’re talking about C++.

Browser other questions tagged

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