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?

Recommended Answers

All 4 Replies

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

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

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

Pretty much.

class AClass
{
    private: /*or protected, depends on your needs*/
                bool truth;
    public:
                bool return_truth();
};
 
bool AClass::return_truth()
{
    return(truth);
}

awesome thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.