Class variables: public access read-only, but private access read/write

后端 未结 12 718
感动是毒
感动是毒 2020-11-28 05:25

Whoopee, not working on that socket library for the moment. I\'m trying to educate myself a little more in C++.

With classes, is there a way to make a variable read-

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 06:19

    You may want to mimic C# properties for access (depending what you're going for, intended environment, etc.).

    class Foo
    {
      private:
        int bar;
    
      public:
        __declspec( property( get = Getter ) ) int Bar;
    
        void Getter() const
        {
          return bar;
        }
    }
    

提交回复
热议问题