Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any real argument for getters/setters instead of public member variables in a simple Point class?

Tags:

c++

I have two simple classes for working with 3d geometry Point and Vector. They both have 3 coordinates as public member variables and define some operators like +, -, * ....

class Point
{
public:
    double x, y, z;
    // ctor and some operators   
}


class Vector
{
public:
    double x, y, z;
    // ctor and some operators   
}

Is there any real argument against making the coordiantes public? I will never change double to any other type. I do not want to restrict the values of x, y, z to a special range and I do not want to check anything when setting the coordinates.

The classes are in a library which will be used by other projects.

Update:
For me a big disadanvtage of setters/getters would be to have to write/read code like this:

myVec.setX(myVec.x() + 1.0);

instead of

myVec.x += 1.0;

Upadate 2:
Qt uses getters/setters in QPoint but with no benefit

like image 894
zboson Avatar asked Dec 15 '18 17:12

zboson


People also ask

Why do we use getters and setters instead of public variables?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.

Should getters and setters be public or private?

Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.

Is it important to always define setters and getters for all the private variables?

It is not necessary to write getter or setter for all private variables. It is just a good practice. But without any public function you can not access the private data(variable) of the class.

Should a class always have getters and setters?

Encapsulation is one of the core concepts of object oriented programming. Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will. Typical example, consider having a dateOfBirth parameter.


2 Answers

In C++, I will add setters and getters if only to conform to the Uniform Access Principle; why should the caller have to keep track of what is stored and what is computed?

When it comes to basic data types, where the only invariant is scope, I will happily admit that the extra effort might not be worth it. But something like a Range type (with .high, .low, .distance) where there is an invariant to be maintained (hight >= low, distance = high - low) then it's a necessity. Otherwise, all the clients of the type end up having to maintain the invariant when that should be the job of the type itself.

like image 75
Daniel T. Avatar answered Sep 19 '22 04:09

Daniel T.


While at first it appears that there is no clear advantage to restrict visibility of class members, it may happen in the future.

For example, if in some future extension you want to add some attribute to the class whose value depend on the x,y,... values, it would be very valuable to have them in a private scope, so that it can get automatically re-computed.

Also, as @vandensh suggest, accessing data members through "getters" and "setters" can help detecting illegal values (see std::isnan ).

There cannot be a clear answer, it really depends on the use case.

like image 30
kebs Avatar answered Sep 22 '22 04:09

kebs