C ++ getters and seters best style

in Java code, simple and obvious in this style:

public: int GetMyAge(){ return myAge; } void SetMyAge(int myAge){ this->myAge = myAge; } private: int myAge; 

(I know that this is "the same thing again," but ). I read most of the related questions on SO, and I still don't know the “best” and “most official” way to do this in C ++. It can't just be a matter of preference, can it ?

EDIT:

It seems like he can .

+2
source share
4 answers

The best style is one that allows you and your team to make quality software that your customers continue to pay for you.

How does this style work for you and your team? Do you find that this causes (or prevents) errors? Is it easy for you to maintain code? Are you bickering about formatting?

Answer these questions, and the answer to your question will arise from them.

+5
source

It’s better not to do this at all. Can your age really change? Blindly providing getters and setters for all properties is a sign that you have not developed correctly.

+11
source

The simple answer is that class names are capital in general in C ++ (except for std classes), methods are lowercase, some frameworks like Qt prefer camelCase, but I prefer underscore_notation - and for example, STL see for example. "Auto_ptr".

Classes do not always have separate .h files, because here the .java file is divided into the .h header (for the whole package) and the .cpp implementation files, one per class.

 class TipicalCamelCase { public: /// mark the frequently used small functions inline in the class def. inline int getMyAge() const; void setMyAge(int myAge=5); // defaults go to the definition. /// for efficiently setting more complex things. void setMyStuff(const MyStuff& myStuff); /// a tipical class-valued getter /// (sometimes scoffed at since it can have memory leaks /// if you dismiss the class but still use and don't copy MyStuff.) const MyStuff& getMyStuff() const; /// a safe getter, but forces copying-out MyStuff. MyStuff getMyStuff() const; private: int myAge; static const int zero=0; // allowed only in the new C++11 standard. static const int one; }; 

Some implementations / initializations (usually in a separate TipicalCamelCase.cpp file):

 const int TipicalCamelCase::one = 1; int TipicalCamelCase::getMyAge() const{ return myAge; } void TipicalCamelCase::setMyAge(int myAge_){ myAge = myAge_; } 

The underline style is the same, but

 int Tipical_camel_case::get_my_age() const { return my_age; } 

I prefer this because it looks cleaner both in the header and in the implementation files. You can see that the function headers are longer than in java. Especially you will see that the templates (generics) header of 2 lines is typical, so it’s worth a little separation.

 template<typename _Tp> int Class_name::general_function(_Tp); 

I think it should be like an introduction to style. If you use inheritance, to work in java style, check every function except virtual constructors so that @overrides rules work correctly.

0
source

What you wrote in the above code is the correct syntax. If you are looking for a thumb rule, write down your acccessor functions so that they are set / get exactly the values.

EG:

 void SetMyAge(int newAge) { if(newAge > 10 && newAge < 100) _age = newAge ; } 

I would prefer to put the validation "newAge> 10 && newAge <100" in another IsValidAge function; even if the code is only one line. Ultimately, small features help maintain the code and help new developers better understand the code.

 void SetMyAge(int newAge) { if(IsValidAge() ) _age = newAge ; } 

However, I would like to comment on this

 void SetMyAge(int myAge){ this->myAge = myAge; } 

It is good practice to distinguish between the varayable class naming convention with the name _myAge.

EDIT I think that the variable name was misunderstood.

myAge should be called _myAge.

-5
source

Source: https://habr.com/ru/post/1447095/


All Articles