C++ getters and setters best style

久未见 提交于 2019-12-21 06:01:07

问题


in Java code convention is simple and obvious, in this style:

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

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

EDIT:

Seems like it can.


回答1:


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

How does this style work for you and your team? Do you find it causes (or prevents) bugs? Do you find it easy to maintain the code? Do you bicker about the formatting?

Answer those questions and the answer to your question will arise out of them.




回答2:


Best not to do it at all. Can your age actually be changed like that? Blindly providing getters and setters for all properties is a sign you have not designed your class properly.




回答3:


A simple answer: class names are capital in general in c++ (except for the std classes), methods are lower case, some frameworks like Qt prefer camelCase, however I prefer underscore_notation -- and so do the STL see eg. "auto_ptr".

Classes do not always have separate .h files, because here a .java file is split up into a .h header (for an entire package), and .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 separate TipicalCamelCase.cpp file):

const int TipicalCamelCase::one = 1;

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

Underscore style is the same but

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

I prefer this as it looks cleaner both in the header and in the implementation files. You can see that function headlines are lengthier than in java. Especially you'll see with templates (generics) 2 lines' header is typical, so it is worth to put them a bit more separated.

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

I think it should do as a style intro. If you use inheritance, for the java-style working, mark every function except the constructors virtual so that the @overrides behave correctly.




回答4:


What you have written in the above code is a correct syntax . If you are looking for a thumb rule, code your acccessor functions in such a way 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 a different function, IsValidAge ; even if the code is just one line. On the long run, small functions help in maintaining the code, and helps new developers to understand the code better.

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 differentiate the nameing convention of the class varaiable to _myAge .

EDIT I think the variable name was comprehended improperly .

myAge should be named _myAge .



来源:https://stackoverflow.com/questions/5788217/c-getters-and-setters-best-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!