0

I'm looking at some source code with a large class, containing lots of getters and setters. All of the setters look generally like this:

void setIntegerVar(const int &var) {local_var = var;}

What is the motivation behind declaring the setter's argument to be an address like this? Why not just

void setIntegerVar(int var) {local_var = var;}

I assume that the answer is because the latter is not an efficient use of memory, and will be copying var somewhere else other than where the caller is storing it. In either case, why should const be necessary?

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
  • 1
    Because this is an `int`, which is a "small" value which is trivial to copy, taking the parameter by value is appropriate, but it was probably written as `const int &` as habit for the types which are not small – Justin Jan 24 '18 at 22:06
  • 3
    It isn't an address. It's a reference. For an integer there isn't much point, but for a million byte-long element, the time savings can be amazing. – user4581301 Jan 24 '18 at 22:06
  • Related: https://stackoverflow.com/q/31402796/1896169 , https://stackoverflow.com/q/21035417/1896169 (although for some of the points made in those Q&A, this [Q](https://stackoverflow.com/q/46549000/1896169) has some relevant discussion too) – Justin Jan 24 '18 at 22:08
  • @user4581301 can you elaborate on the distinction? – pretzlstyle Jan 24 '18 at 22:08
  • Please fix your title. It says 'getters' but your question is about 'setters'. – user207421 Jan 24 '18 at 22:13
  • @Anonymous **void setIntegerVar(int var)..** takes the parameter by value which means it copies it. However in **void setIntegerVar(const int &var)...** it takes the reference. For POD types since they're relatively smaller in size it doesn't differ much but for user-defined types e.g with lots of member variables as they are bigger in size, pass-by-value will copy the whole object wheras pass-by-reference to will just copy its address which saves time and memory – Onur A. Jan 24 '18 at 22:14
  • A reference is an alias. A different name for something that already exists. There may or may not be any storage associated with the reference (the storage used by the variable referenced is another matter). A pointer is a variable that can store the location of another variable. For the nitty gritty details give this a read: [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – user4581301 Jan 24 '18 at 22:16
  • 2
    Here's an example where passing by reference makes a big difference: https://www.reddit.com/r/cpp/comments/7r2e2j/question_about_surprising_performance_benchmark/?ref=share&ref_source=link – Justin Jan 24 '18 at 22:16
  • Side note on setters: A setter that is public and does nothing to ensure the provided value causes no harm to the object or provide some other service is useful only as a place to hang a debugger breakpoint. By itself this is a good thing, but if you approach from the goal of encapsulation, encapsulation has been broken. Anyone can set the value to anything and there's nothing the object can do to defend itself. A good setter is gatekeeper protecting the object from harm, even if that's only by announcing the change. – user4581301 Jan 24 '18 at 22:28
  • http://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html – 2785528 Jan 24 '18 at 23:42
  • https://martinfowler.com/bliki/TellDontAsk.html – 2785528 Jan 24 '18 at 23:42

0 Answers0