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

后端 未结 12 544
感动是毒
感动是毒 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:05

    The only way I know of granting read-only access to private data members in a c++ class is to have a public function. In your case, it will like:

    int getx() const { return x; }

    or

    int x() const { return x; }.

    By making a data member private you are by default making it invisible (a.k.a no access) to the scope outside of the class. In essence, the members of the class have read/write access to the private data member (assuming you are not specifying it to be const). friends of the class get access to the private data members.

    Refer here and/or any good C++ book on access specifiers.

    0 讨论(0)
  • 2020-11-28 06:06

    As mentioned in other answers, you can create read only functionality for a class member by making it private and defining a getter function but no setter. But that's a lot of work to do for every class member.

    You can also use macros to generate getter functions automatically:

    #define get_trick(...) get_
    #define readonly(type, name) \
    private: type name; \
    public: type get_trick()name() {\
        return name;\
    }
    

    Then you can make the class this way:

    class myClass {
        readonly(int, x)
    }
    

    which expands to

    class myClass {
        private: int x;
        public: int get_x() {
            return x;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:08

    Of course you can:

    class MyClass
    {
        int x_;
    
    public:
        int x() const { return x_; }
    };
    

    If you don't want to make a copy (for integers, there is no overhead), do the following:

    class MyClass
    {
        std::vector<double> v_;
    
    public:
        decltype(v)& v() const { return v_; }
    };
    

    or with C++98:

    class MyClass
    {
        std::vector<double> v_;
    
    public:
        const std::vector<double>& v() const { return v_; }
    };
    

    This does not make any copy. It returns a reference to const.

    0 讨论(0)
  • 2020-11-28 06:08

    While I think a getter function that returns const T& is the better solution, you can have almost precisely the syntax you asked for:

    class myClass {
        private:
        int x_; // Note: different name than public, read-only interface
    
        public:
        void f() {
            x_ = 10; // Note use of private var
        }
        const int& x;
        myClass() : x_(42), x(x_) {} // must have constructor to initialize reference
    };
    
    int main() {
        myClass temp;
    
        // temp.x is const, so ...
        cout << temp.x << endl; // works
        // temp.x = 57;  // fails
    
    }
    

    EDIT: With a proxy class, you can get precisely the syntax you asked for:

    class myClass {
    public:
    
        template <class T>
        class proxy {
            friend class myClass;
        private:
            T data;
            T operator=(const T& arg) { data = arg; return data; }
        public:
            operator const T&() const { return data; }
        };
    
        proxy<int> x;
        // proxy<std::vector<double> > y;
    
    
        public:
        void f() {
            x = 10; // Note use of private var
        }
    };
    

    temp.x appears to be a read-write int in the class, but a read-only int in main.

    0 讨论(0)
  • 2020-11-28 06:12

    Write a public getter function.

    int getX(){ return x; }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题