hi all~
i wanna know how to make some members to be read-only ones in class implementation, maybe simple question ?
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~
What do you mean by read only?
Woop?
Well, read only means that the data can only be read from, not written to.Originally Posted by prog-bman
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
Its always the easy questions that I over analyze and make myself fell real dumb
Woop?
it is wonderful but i can not understand it utterly can you explain in more details ? thanx !Originally Posted by Sang-drax
Never end on learning~
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:Originally Posted by black
[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]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
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
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~
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:
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."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; }
"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