Flavius Flavius - 1 month ago 18
C# Question

Does C++11 have C#-style properties?

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


In C++ I have to write

private:
Foo foo;
public:
Foo getFoo() { return foo; }


Is there some such concept in the C++11 allowing me to have some syntax sugar on this?

Answer

No, it doesn't.

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.