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