const object in c++
- by Codenotguru
I have a question on constant objects. In the following program:
class const_check{
int a;
public:
const_check(int i);
void print() const;
void print2();
};
const_check::const_check(int i):a(i) {}
void const_check::print() const {
int a=19;
cout<<"The value in a is:"<<a;
}
void const_check::print2() {
int a=10;
cout<<"The value in a is:"<<a;
}
int main(){
const_check b(5);
const const_check c(6);
b.print2();
c.print();
}
void print() is constant member function of the class const_check, so according to the definition of constants if any attempt to change int a it should result in an error but the program works fine for me.I think i am having some confusion here, can anybody tell me why the compiler is not flagging it as an error??