C ++ Getters-Setters in implementation file

I am relatively new to C ++, and I think that my question can be best examined with an example. Suppose in my header file

class myClass{ public: double getVar1(); void setVar1(double newVar1); void copyVar1(myClass* dat); private: double var1; }; 

In my .cc implementation file, when implementing the copyVar1 method, should I do

 void myClass::copyVar1(myClass* dat){ var1 = dat->var1; } 

or

 void myClass::copyVar1(myClass* dat){ var1 = dat->getVar1(); } 

where in the second case, I use the getter method instead. Both work correctly in Visual C ++, but I would like to know what is best used in practice.

Thanks for your comments!

+7
source share
8 answers

Best practics? Overload the assignment operator instead of writing a method.

 myClass & myClass::operator=(const myClass & dat) { var1 = dat.var1; // or dat.getVar1() return *this; } 

As for using a field or calling a setter ... it's all a matter of personal taste. If your recipient has a side effect, you should probably trigger it, otherwise use the field.

So, a big "depends".

+4
source

You should almost always use getter / setter methods to access a variable when you are outside the class, and often you have to do this because this is the only way to do this. However, when you are inside a class, you can use it, and if the getter method returns nothing but returns a variable, this will not change the situation.

Your decision will be based on if you have code in the getter method that does something more than just returning a variable, and if you want this code to execute when copyVar1 called . If you don’t know, my advice would be to use the getter method if you ever decide to change the code in it in the future. Despite the fact that it works fine now, just by contacting it directly, and you may have microscopically better performance, it is much easier to find an error when calling the recipient, if you should not call it when you want. And the compiler will probably ultimately optimize so you don’t even feel the difference .: D

+2
source

I would prefer to use setters and getter this way, if you change some implementation details (e.g., validate when assigning a value), you should change this in one place in setter ...

+1
source

It depends. Many people will tell you that using getters / setters instead of variables is more resistant to programming errors and changes and reduces code duplication. But as long as these getters / setters are not built-in functions, you can get little success and, since the implementation must know the internal elements of the class, why not just use the variables directly.

0
source

Are you trying to write a copy constructor? Why do you need copyVar1 when you have setVar1? If you are trying to write a copy constructor, it is best not to use getter.

 myclass(const myclass& other) { var1 = other.var1; } 

When you have both getter and setter, why not make public var1?

0
source

There is no right or wrong way to do it here.

However, you assume that getVar1 () returns the value or var1. If you decide to change the way you store the value of var1, you need to go through all the code and update it. Using setter / getter reduces this to a few methods. In this case, you should use the third option:

 void myClass::copyVar1(myClass* dat){ setVar1 (dat->getVar1()); } 

and then you completely remove the var1 dependency, you can change var1 to something else, and copyVar1 will still work.

0
source

As you say, both work correctly and are completely legal.

In a sense, the difference between accessing a member variable or using access is that you define a stronger “layer” inside your class; this layer, like any level, helps you reduce dependencies.

In other words, the getter method basically meets the requirement not to reveal to the outside world the implementation detail; let's say one day you can decide that instead of saving var1 as a double, you calculate it. Thus, the accessor gives you such freedom: to some extent, you can change the implementation without affecting other classes (i.e., reducing dependencies).

The same is true when using the getter method inside the class itself, i.e. you remove dependencies within the class, and your design is more resilient to change.

0
source

The point of abstraction of setters and getters is that the extraction method can change. When you access a member variable directly, then if something changes, you should update your usage everywhere. But if you use this function, then changing the function can only be changed in the function.

Now, since this is the same class, it is much less dangerous than when you make the variables public, because you only need to update this single implementation file (instead of updating each user of your class, which may not be possible if you have there is a library class).

To see this, look at a class that should suddenly become thread safe. Now your copier will look like

 void myClass::copyVar1(myClass * dat) { scoped_lock lock(mutex); var1 = dat->getVar1(); } 

and you don’t have to fix it anywhere if you always called the function. If you accessed a variable directly, it would look like

 void myClass::copyVar1(myClass * dat) { scoped_lock lock(mutex); scoped_lock lock2(dat->mutex) var1 = dat->var1; } 

which blocks the "mutex" dat, external to dat, is usually not considered a very good design (if you make engineers remember to block other people's objects, then you open the chance that they forget and do not).

Similarly, if a variable is stored in a database, you will also have to process it. Or, if now the variable was saved in two different variables and built when it was restored, you can see how complexity will increase.

But remember, the design philosophy for using accessories and mutators is to reduce potential complexity through encapsulation. There are times when you work on small projects (especially with personal projects) where you do not intend to change the class. Perhaps it would be less difficult to access them directly. Do not be afraid to do this, just know why you are making a decision.

0
source

All Articles