In structures (struct) is it necessary to use getters and setters or only in classes (class)?

4

Is it necessary to use setters and getters in structures to maintain good practices or is it only necessary in classes?

    
asked by anonymous 13.12.2017 / 23:28

3 answers

5

Not needed in either. And this business of good practice is a fallacy.

For example, in C ++ it is not common to use this kind of mechanism. There are those who use them, but in specific application types and they would usually look better in another language.

There's room to do this, but C ++ programmers are often above average and know that in many cases it's not necessary. They do not follow good practices, they understand everything as it works, and know the requirements properly, and do not follow cake recipe. Every time you do something because it's good practice, you're already doing it wrong, even if it works. If you do it because you correctly and completely understand that it is necessary in that context, there is a good chance of being right.

A structure and a class is essentially the same thing in C ++, you can even use it differently, but technically there is no difference other than the visibility pattern of the members.

C ++ is a more dogmatic language, it allows you to do everything, but expects you to only use what you really need.

I usually say, if you do not understand all the implications of using a mechanism, do not use it. The site is full of information on the subject in several languages. It works the same in all, the difference is only cultural and frequency that it is necessary.

    
13.12.2017 / 23:45
1

Not needed in either, but by default, in% with% s, the attributes are public. This means that if you have a struct and you want to access one of the attributes of one of its copies from the outside, you need to set the attribute to class or create the accessor and modifier (getter and setter) for him.

    
16.12.2017 / 04:11
1
___ erkimt ___ In structures (struct) is it necessary to use getters and setters or only in classes (class)? ______ qstntxt ___

Is it necessary to use setters and getters in structures to maintain good practices or is it only necessary in classes?

    
______ azszpr262767 ___

Not needed in either. And this business of good practice is a fallacy.

For example, in C ++ it is not common to use this kind of mechanism. There are those who use them, but in specific application types and they would usually look better in another language.

There's room to do this, but C ++ programmers are often above average and know that in many cases it's not necessary. They do not follow good practices, they understand everything as it works, and know the requirements properly, and do not follow cake recipe. Every time you do something because it's good practice, you're already doing it wrong, even if it works. If you do it because you correctly and completely understand that it is necessary in that context, there is a good chance of being right.

A structure and a class is essentially the same thing in C ++, you can even use it differently, but technically there is no difference other than the visibility pattern of the members.

C ++ is a more dogmatic language, it allows you to do everything, but expects you to only use what you really need.

I usually say, if you do not understand all the implications of using a mechanism, do not use it. The site is full of information on the subject in several languages. It works the same in all, the difference is only cultural and frequency that it is necessary.

    
______ azszpr263428 ___

Not needed in either, but by default, in% with% s, the attributes are public. This means that if you have a %code% and you want to access one of the attributes of one of its copies from the outside, you need to set the attribute to %code% or create the accessor and modifier (getter and setter) for him.

    
______ ___ azszpr263426
The use of getters and setters is neither required nor required in either case, but as you mentioned, good practices help both in encapsulation, and to facilitate the adaptation of the variable value received in which it is stored, for example, performing calculations and providing a string have received only two digits.

    
___
16.12.2017 / 03:50