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

前端 未结 15 1088
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 03:16

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 fo         


        
15条回答
  •  一向
    一向 (楼主)
    2020-11-28 03:40

    This is not exactly a property, but it does what you want the simple way:

    class Foo {
      int x;
    public:
      const int& X;
      Foo() : X(x) {
        ...
      }
    };
    

    Here the big X behaves like public int X { get; private set; } in C# syntax. If you want full-blown properties, I made a first shot to implement them here.

提交回复
热议问题