问题描述
在C#中,有一个很好的语法糖与getter和setter领域。此外,我喜欢自动实现属性允许我写
In C#, there is a nice syntax sugar for fields with getter and setter. Moreover, I like the auto-implemented properties which allow me to write
public Foo foo { get; private set; }
在C ++中我必须写
private:
Foo foo;
public:
Foo getFoo() { return foo; }
有没有在C ++ 11一些这样的概念让我有这方面的一些语法糖?
Is there some such concept in the C++11 allowing me to have some syntax sugar on this?
推荐答案
没有,没有。
这说,C ++的哲学是促进的封装的。当你发现你的界面刚刚经历成员元素传递(getter和setter),再想一想。您的类应该有一个有意义的接口的什么类做的,而不是仅仅作为一个东西松容器。成员对象应该是一个实现细节,而不是是公共接口的一部分。如果你想有一个宽松的容器,只需要使用的std ::元组
或类似的东西。
That said, the philosophy of C++ is to promote encapsulation. Whenever you find your interface just passing through member elements ("getters and setters"), think again. Your class should have a meaningful interface for what the class does, rather than just serve as a loose container for stuff. Member objects should be an implementation detail rather than be part of public interface. If you want a loose container, just use a std::tuple
or something like that.
这篇关于难道C ++ 11有C#风格的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!