-1

Just like in Qt , I saw in QPoint class. It use QPoint.x() to get and use QPoint.setX() to set. why not just use x as a public variable? I know some book recommend it, but what's the deep reason about it? and it's not so convenient sometimes.

vito_yu
  • 63
  • 5

2 Answers2

3

There are three basic reasons for doing this.

  1. Abstraction. Your user shouldn't know or care if x is stored by value or calculated every time. Maybe you store the upper x coordinate of a box and its width, and calculate x as max_x - width. Maybe you change how you store the data in version 3.4.
  2. Consistency. Your user shouldn't have to remember to call x as a variable (myobject.x) while calling width as a function (myobject.width()). So make it easy, and have everything (within reason) as a function.
  3. Control. Maybe x is only allowed to be positive, or even, or when you change x, upper_x also needs to change. With setters/getters, you're able to enforce all of this.
Dave
  • 44,275
  • 12
  • 65
  • 105
  • but for some variables like the x,y,z,w cordinates in a vector class, it seems like a waist of memory on the stack to have a getter. is this wrong? – Anthony Raimondo Mar 27 '13 at 01:33
  • @JavaNewb The getter will be optimised out by the compiler 99.99% of the time. See my comment on sftrabbit's post for possible reasoning behind using functions. Also don't worry about wasting stack memory with trivial things like this; if you reach a point where 8 bytes on the stack will break your app, it's already broken. – Dave Mar 27 '13 at 01:35
1

There are three common reasons to make a member private and access it with getters and setters:

  1. You need to maintain some invariant over the member. That is, if there is some value that the member can have that would make your object invalid, you make sure the client can't just modify it to their own will. Stroustrup recommends using a struct if you have no invariants.

  2. You want the interface to expose the member in some different form. An example is if you store a Person's dateOfBirth but you want to expose it through a member function getAge(). The age is calculated from the data of birth, but you don't want clients to access dateOfBirth directly.

  3. At some point in the future, it's possible that one or both of the previous two points will become true.

If I were implementing a Point, I'd make it a struct and have x and y be public members. There's simply no such thing as an invalid Point and I can't imagine the coordinates would ever need to be calculated from some other representation. The Qt developers obviously decided otherwise though.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • 1
    On the last point you raise; I would have `x` and `y` be functions as Qt did. The reasoning: that point is a 2D version of something which can be n-D. The logical way of storing that data is in an array, so my class would have a `coord[2]` variable. `x` and `y` would then be aliases for each index of that array, while all internal functions (addition, for example) would use array indices and looping (to be optimised at compile time). This means that if I later add 3D functionality, I can use a templated `Point` for both 2D and 3D, and higher dimensions if that's ever useful. – Dave Mar 27 '13 at 00:33