在C ++中访问器方法(getter和setter)的约定 [英] Conventions for accessor methods (getters and setters) in C++

查看:111
本文介绍了在C ++中访问器方法(getter和setter)的约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关C ++中的访问器方法的几个问题已经被问到SO,但没有一个能够满足我的好奇心的问题。



我尽量避免访问器,因为,像Stroustrup和其他着名的程序员,我认为一个类,其中许多是坏OO的迹象。在C ++中,我在大多数情况下可以向类添加更多的职责或使用friend关键字来避免它们。



有几种可能性:



强> 1。根本不要使用访问器。



我们可以将相应的成员变量公开。这是Java中的禁忌,但似乎与C ++社区没有关系。但是,我有点担心case是一个显式副本或只读(const)引用一个对象应该返回,是夸张吗?



<强> 2。使用Java式get / set方法



我不知道它是否来自Java,但我的意思是:

  int getAmount(); //返回金额
void setAmount(int amount); //设置金额

3。使用目标C风格的get / set方法



这有点奇怪,但显然日益常见:

  int amount(); //返回金额
void amount(int amount); //设置金额

为了使它工作,你必须找到一个不同的名称您的成员变量。有些人附加下划线,其他人在前面附加m_。

解决方案



div>

从我的角度来看,从维护角度看,有四百万行C ++代码(而这只是一个项目):




  • 如果成员是不可变的(即 const )或者没有依赖的简单类型


  • 如果成员 private ,它也可以跳过getters / setters。我还将内部 pimpl 类的成员计为 private ,如果


  • 如果会员 public protected protected public - const ,非简单或依赖关系然后使用getters / setters。




作为一个维护人员我想要有getters / setters的主要原因是因为我有一个地方放断裂点/日志/别的东西。



我喜欢替代2的风格,因为它更可搜索(编写可维护代码的关键组件)。


Several questions about accessor methods in C++ have been asked on SO, but none was able satisfy my curiosity on the issue.

I try to avoid accessors whenever possible, because, like Stroustrup and other famous programmers, I consider a class with many of them a sign of bad OO. In C++, I can in most cases add more responsibility to a class or use the friend keyword to avoid them. Yet in some cases, you really need access to specific class members.

There are several possibilities:

1. Don't use accessors at all

We can just make the respective member variables public. This is a no-go in Java, but seems to be OK with the C++ community. However, I'm a bit worried about cases were an explicit copy or a read-only (const) reference to an object should be returned, is that exaggerated?

2. Use Java-style get/set methods

I'm not sure if it's from Java at all, but I mean this:

int getAmount(); // Returns the amount
void setAmount(int amount); // Sets the amount

3. Use objective C-style get/set methods

This is a bit weird, but apparently increasingly common:

int amount(); // Returns the amount
void amount(int amount); // Sets the amount

In order for that to work, you will have to find a different name for your member variable. Some people append an underscore, others prepend "m_". I don't like either.

Which style do you use and why?

解决方案

From my perspective as sitting with 4 million lines of C++ code (and that's just one project) from a maintenance perspective I would say:

  • It's ok to not use getters/setters if members are immutable (i.e. const) or simple with no dependencies (like a point class with members X and Y).

  • If member is private only it's also ok to skip getters/setters. I also count members of internal pimpl-classes as private if the .cpp unit is smallish.

  • If member is public or protected (protected is just as bad as public) and non-const, non-simple or has dependencies then use getters/setters.

As a maintenance guy my main reason for wanting to have getters/setters is because then I have a place to put break points / logging / something else.

I prefer the style of alternative 2. as that's more searchable (a key component in writing maintainable code).

这篇关于在C ++中访问器方法(getter和setter)的约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆