C++ getter 和 setter 最佳风格

发布于 2024-11-03 12:14:50 字数 370 浏览 4 评论 0原文

Java代码约定简单明了,采用这种风格:(

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

我知道它“又是同一件事”,但是)我已经阅读了大多数相关问题,但我仍然不知道“用 C++ 实现这一点的最佳方法”和“最官方的”方法。这不可能只是一个偏好问题,可以吗?

编辑:

似乎可以

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甚是思念 2024-11-10 12:14:50

最好根本不要这样做。年龄真的可以这样改变吗?盲目地为所有属性提供 getter 和 setter 是您没有正确设计类的标志。

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.

黯淡〆 2024-11-10 12:14:50

最好的风格是让您和您的团队能够开发出高质量的软件,并且您的客户会继续为您付费。

这种风格对你和你的团队来说效果如何?您发现它会导致(或防止)错误吗?您觉得维护代码容易吗?您是否对格式争论不休?

回答这些问题,你的问题的答案就会从中产生。

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.

℉服软 2024-11-10 12:14:50

一个简单的答案:在 C++ 中,类名通常是大写的(除了 std 类),方法是小写的,一些框架(如 Qt)更喜欢驼峰式命名法,但我更喜欢下划线符号——STL 也是如此。 “auto_ptr”。

类并不总是有单独的 .h 文件,因为这里 .java 文件被分割成 .h 标头(对于整个包) 和 .cpp 实现文件,每个类一个。

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;
};

一些实现/初始化(通常在单独的 TipicalCamelCase.cpp 文件中):

const int TipicalCamelCase::one = 1;

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

下划线样式是相同的,但

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

我更喜欢它,因为它在标头和实现文件中看起来更干净。
您可以看到函数标题比 java 中的更长。特别是您会看到 templates (泛型)的 2 行标题是典型的,因此值得将它们分开一些。

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

我认为它应该作为风格介绍。
如果您使用继承,对于 java 风格的工作,请标记除构造函数 virtual 之外的每个函数,以便 @overrides 行为正确。

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.

还如梦归 2024-11-10 12:14:50

您在上面的代码中编写的语法是正确的。如果您正在寻找经验法则,请以正确设置/获取值的方式对访问器函数进行编码。

EG:

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

我更愿意将验证“newAge > 10 && newAge < 100”放在不同的函数 IsValidAge 中;即使代码只有一行。从长远来看,小函数有助于维护代码,并帮助新开发人员更好地理解代码。

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

不过,我想对此发表评论,

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

将类变量的命名约定与 _myAge 区分开来是一种很好的做法。

编辑
我认为变量名的理解不正确。

myAge 应命名为 _myAge 。

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文