返回成员变量的引用是否为pratice?

编程入门 行业动态 更新时间:2024-10-28 19:26:49
本文介绍了返回成员变量的引用是否为pratice?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面说的更好,然后有第一/第二作为公共成员。我相信这是几乎一样糟糕。如果你给一个方法访问一个私人变量在类外面然后什么是点?不应该的函数

The below is said to be better then having first/second as public members. I believe this is nearly as bad. If you're giving a way to access a private variable outside of the class then whats the point? Shouldn't the functions be

T First(); void(or T) First(const T&)

示例:

// Example 17-3(b): Proper encapsulation, initially with inline accessors. Later // in life, these might grow into nontrivial functions if needed; if not, then not. // template<class T, class U> class Couple { Couple() : deleted_(false) { } T& First() { return first_; } U& Second() { return second_; } void MarkDeleted() { deleted_ = true; } bool IsDeleted() { return deleted_; } private: T first_; U second_; bool deleted_; };

推荐答案

到一个类的内部是坏的。从(我认为最重要的)开始:

There are several reasons why returning references (or pointers) to the internals of a class are bad. Starting with (what I consider to be) the most important:

  • 封装泄漏一个实现细节,这意味着你不能再根据需要改变你的类内部。例如,如果你决定不存储 first _ ,但是要实时计算它,你将如何返回一个引用呢?

  • Encapsulation is breached: you leak an implementation detail, which means that you can no longer alter your class internals as you wish. If you decided not to store first_ for example, but to compute it on the fly, how would you return a reference to it ? You cannot, thus you're stuck.

    不变性不再可持续(在非常量引用的情况下):anybody可以随意访问和修改引用的属性,因此您不能监视其更改。这意味着您不能维护此属性所属的不变量。

    Invariant are no longer sustainable (in case of non-const reference): anybody may access and modify the attribute referred to at will, thus you cannot "monitor" its changes. It means that you cannot maintain an invariant of which this attribute is part. Essentially, your class is turning into a blob.

    生命周期问题很容易保持一个引用或指向属性在它们属于的原始对象停止存在之后。这当然是未定义的行为。例如,大多数编译器会尝试警告保持对堆栈上的对象的引用,但是我知道没有编译器能够为函数或方法返回的引用产生这样的警告:你自己。

    Lifetime issues spring up: it's easy to keep a reference or pointer to the attribute after the original object they belong to ceased to exist. This is of course undefined behavior. Most compilers will attempt to warn about keeping references to objects on the stack, for example, but I know of no compiler that managed to produce such warnings for references returned by functions or methods: you're on your own.

    因此,通常最好不要传递引用或指向属性的指针。

    As such, it is usually better not to give away references or pointers to attributes. Not even const ones!

    对于较小的值,一般足以通过复制( in 和 out ),特别是现在使用move语义(在途中)。

    For small values, it is generally sufficient to pass them by copy (both in and out), especially now with move semantics (on the way in).

    对于更大的值,它真的取决于情况,有时一个代理可以缓解你的烦恼。

    For larger values, it really depends on the situation, sometimes a Proxy might alleviate your troubles.

    最后,请注意,对于一些类,有公共成员不是那么坏。封装对的成员的意义是什么?当你发现自己写的类只是一个属性的集合(没有不变的任何东西),然后,而不是得到所有的OO对我们,并为他们写一个getter / setter对,考虑使它们公开。

    Finally, note that for some classes, having public members is not so bad. What would be the point of encapsulating the members of a pair ? When you find yourself writing a class that is no more than a collection of attributes (no invariant whatsoever), then instead of getting all OO on us and writing a getter/setter pair for each of them, consider making them public instead.

  • 更多推荐

    返回成员变量的引用是否为pratice?

    本文发布于:2023-07-30 16:16:09,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1250967.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:变量   成员   pratice

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!