English 中文(简体)
是否恢复供养人参考?
原标题:Is returning a reference for accessor idiomatic?

在C++中,可以设立一个接驳人,返回私人领域。

class Cls {
    private:
        int _attr;
    public:
        int& attr() { return _attr; }
};

such that the attribute can be accessed as such:

// set
c.attr() = 4;

// get
cout << c.attr() << endl;

这种从属/良好的做法吗? 平均C++方案管理员是否惊讶地看到这种进入者的风格? (Hint:我第一次看到这一点感到惊讶,但就像风格一样。)

最佳回答

无,只要你还提供<条码>-条码/条码>超载,该代码即为无抵押:

class Cls {
    int _attr;
public:
    int& attr() { return _attr; }
    int const& attr() const { return _attr; }
};

然而,出于克里斯·卢茨和马克·布提到的理由,我将考虑以下属性:

class Cls {
    int _attr;
public:
    int const& attr() const { return _attr; }
    void attr(int i) { _attr = i; }
};
问题回答

Suppose you needed guarantees:

template <int Base>
class Digit {
  private:
    int attr_; // leading underscore names are reserved!
  public:
    int attr() const { return attr_; }
    void attr(int i) { attr_ = i % Base; } // !
};

在这种情况下,你可以回头看,因为没有办法保证用户以你不想要的方式赢得修改。 这是使用一套装置的主要原因——你可以(现在或稍后日期)过滤输入,以确保其能够被接受。 提到你必须盲目接受用户指派给你的成员的内容。 此时此刻,为什么不只使之公开?

In general, for non-trivial classes (for example ones that can be defined simply as a struct) providing accessors/mutators to specific attributes is probably a code smell. Generally, classes should tend to keep their internal state just that: Internal. If you provide a non-const reference to internal state then all of a sudden you have no control over class invariants at all. This will not only make debugging significantly harder since the scope of possible state changes is the entire project, it prevents you from ever changing the internals of your class because they re actually both state AND user API.

相反,你或许应当制定方法,在保持班级不变的同时,对你的班级进行有意义的工作。 在某些情况下,只提供读取特定国家变量是完全正确的,而在另一些情况下,你只想以另一种方式提供最终状态。

这样做完全打败了首先使其为私自私的目的。

进入者的目的是暴露内部状态,这样,当外部法典试图改变国家时,该阶级就可以维持不变(要么只允许读取,要么在修改国家之前确认书面准入);他们通常会看像什么。

int  attr() const {return _attr;}
void attr(int a)  {_attr = a;}

如果今后你需要限制属性所能达到的价值观,或改变内部储存的方式,那么,你就可以在不改变类别接口的情况下修改这些要素的实施。

If this is not necessary (and you have decided that it will never be necessary), just make it public; exposing it as a reference gains nothing but a few lines of unnecessary code and a quirky syntax when using it.

It depends. If the role of the class is to contain such objects (e.g. a container class), then its very idiomatic, and the normal way of doing things. In most other cases, however, it is considered preferrable to use getter and setter methods. Not necessarily named getXxx and setXxx---the most frequent naming convention I ve seen uses m_attr for the name of the attribute, and simply attr for the name of both the getter and the setter. (Operator overloading will choose between them according to the number of arguments.)

-- James Kanze

提到裁军厅成员的情况通常并不常见。 我认为,必须读一下的人会期望为此目的建立一个小组。 然而,如果你这样做,也不要忘记把它上载到<条码>const上。

它相对频繁,但却带有坏风格。

例如,如果你看一下STL,你就说,产生<代码>const&或的类别;到国内状态的类型通常是便衣集装箱,只能为你实际储存的集装箱提供这种准入。 显然,你无法直接改变诸如<代码>size<>/code>或双向树的内部节点等特性。

提供直接接触元素的机会打破了cap头。 不仅你失去所有阶层对这些因素的束缚,而且你也使这些因素成为您的“意向书”的一部分,因此,如果不对所有客户进行更新,就无法改变这一类的执行情况。

就我而言,它们是C++的两类:

struct BigBag {
  Foo _foo;
  Bar _bar;
  FooBar _foobar;
};

class MyClass {
public:
  explicit MyClass(int a, int b);

  int getSomeInfo();

  void updateDetails(int a, int b);
private:
  // no peeking
};

也就是说,要么仅仅收集相关内容,在这种情况下,所有内容都是公开的,要么是重复的,要么是因为有许多客户代码,而在这种情况下,你并不暴露执行细节。

就像你想要摆脱父母的he一样......

class Cls {
    private:
        int _attr;
    public:
        Cls() : attr(_attr) { }
        int& attr;
};

Edit:Chris观点很好。 基本上没有任何东西从公开查阅的私人变量总结出来。 下面的版本的确增加了一些内容。 提供参考资料会防止出现,但可生成私人变量。

class Cls {
    private:
        int _attr;
    public:
        Cls() : attr(_attr) { }
        int const & attr;
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签