Thread: how to make read-only members ?

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    how to make read-only members ?

    hi all~

    i wanna know how to make some members to be read-only ones in class implementation, maybe simple question ?
    Never end on learning~

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    What do you mean by read only?
    Woop?

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by prog-bman
    What do you mean by read only?
    Well, read only means that the data can only be read from, not written to.

    To answer the first posters question, declare the members const:
    Code:
     class A
     {
       A() : data(1) //set data to 1
       {
       }
       
       const int data;
     }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Its always the easy questions that I over analyze and make myself fell real dumb
    Woop?

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by Sang-drax
    Well, read only means that the data can only be read from, not written to.

    To answer the first posters question, declare the members const:
    Code:
     class A
     {
       A() : data(1) //set data to 1
       {
       }
       
       const int data;
     }
    it is wonderful but i can not understand it utterly can you explain in more details ? thanx !
    Never end on learning~

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by black
    it is wonderful but i can not understand it utterly can you explain in more details ? thanx !
    The class contains a const data member that provides your "read-only" requirement. The constructor demonstrates how to initialize this member data with a value 1. If you want to, you can have a constructor such that you can provide this member a value of your choosing upon creation of an instance of that class:

    Code:
    class A
    {
        const int data;
    public:
        A(const int val=1) : data(val)
        {
        }
    };
    ...
    A a1;     // class member 'data' for a1 gets initialized to default value of 1
    A a2(4);  // class member 'data' for a2 gets initialized to value of 4
    [edit]Perhaps you have a different idea for what you are thinking of regarding a read-only class member? The designer of a class can in essence make the data within read-only by how he chooses to implement the classes' public/private interface.[/edit]
    Last edited by hk_mp5kpdw; 08-11-2004 at 06:43 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx man i got it, but another problem, what if we prefer that read-only member not to be static(ie. constant) but dynamic one ? for example we may implements a LoadComponents class to load external source in and which has a read-only property named loaded to indicate whether all things are ready, it will be set to false at first, and automatically be true when everything are loaded, so this loaded property can not be constant but aother way, any ideas ?
    Never end on learning~

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It looks like what you want is to have a private member variable. During construction of your class you can initialize this to false. When you load in your data you can then set this to true. A private class member can only be modified by the class' own member functions, it is hidden/inaccessible from direct access by the user. All functions/data members of a class are private by default unless you specify otherwise:

    Code:
    class LoadComponents
    {
        bool loaded1;          // By default this is private to the class
    public:                    // Now everything after this point is public
        bool loaded2;          // This is a public data member
        LoadComponents()
        {
            loaded1 = false;
            loaded2 = false;
        }
        SetLoaded1(bool value)
        {
            loaded1 = value;   // Class member function can set value here
        }
    };
    ...
    int main()
    {
        LoadComponents comp1;
       
        comp1.loaded1 = true;  // Won't work because loaded1 is private
        comp1.loaded2 = true;  // Will work because loaded2 is public
    
        comp1.SetLoaded1(true);// After this point, loaded1 will be true
        return 0;
    }
    The variables in the class aren't really read-only but the public/private interface can help to hide or prevent unauthorized access. In the class above, the only way to change loaded1 is by calling the function SetLoaded1, we cannot directly access it simply by saying comp1.loaded1, we are forced to go through the public interface to affect any changes. BTW be careful with your use of the word static because as the great Inigo Montoya once said, "I do not think it means what you think it means."
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  2. Replies: 15
    Last Post: 10-08-2005, 04:36 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. how to make parallel port read data
    By lwong in forum Windows Programming
    Replies: 0
    Last Post: 01-05-2004, 08:14 PM
  5. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM