0

how would I go about making a variable in a class, that could be read directly but not written as such?
Like,

if(class1.variable==true){
}

is allowed but class1.variable = true; is not...
however I don't want a const , I want it to be able to be set from within the class, just not directly. How would I do this?

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by winrawr
0

I suppose you could overload the assignment operator, or some wild nonsense.... why not just make a getter method, and properly encapsulate the variable?

0

"encapsulate" the variable?
And do you mean like,

bool class1::class1.variable(){
    return variable;
}
0

Pretty much.

class AClass
{
    private: /*or protected, depends on your needs*/
                bool truth;
    public:
                bool return_truth();
};
 
bool AClass::return_truth()
{
    return(truth);
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.