Thread: read-only data members. A question of good practices

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    read-only data members. A question of good practices

    Code:
    class myClass {
    public:
        myClass(): val(0), value(val) {}
        myClass(int x): val(x), value(val) {}
        void set_value(int x) { val = x; }
    private:
        int val;
    public:
        const int &value;
    };
    The above is a minimalist example to illustrate my question...

    Instead of coding getters for every data member, is the const reference considered a correct approach within the context of good programming practices under C++?

    EDIT: I understand this forces me to overload the assignment operator. But still... is this a common practice?
    Last edited by Mario F.; 06-18-2006 at 06:34 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. Client-Server Data Read Problem
    By bob2509 in forum C Programming
    Replies: 8
    Last Post: 11-06-2002, 11:47 AM
  5. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM