Non constant member function being called inside a constant member function
#include <iostream>
class Hello {
public:
void Test() {
std::cout << "Testing" << std::endl;
}
};
class Hi {
public:
Hi()
:hello(new Hello())
{}
~Hi()
{
delete hello;
}
void Testing() const {
hello->Test();
}
private:
Hello * hello;
};
int main(int argc, char ** argv) {
Hi hi; ;
hi.Testing();
return 0;
}
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .
Re: Non constant member function being called inside a constant member function
Quote:
Originally Posted by
vikuseth
Code:
#include <iostream>
class Hello {
public:
void Test() {
std::cout << "Testing" << std::endl;
}
};
class Hi {
public:
Hi()
:hello(new Hello())
{}
~Hi()
{
delete hello;
}
void Testing() const {
hello->Test();
}
private:
Hello * hello;
};
int main(int argc, char ** argv) {
Hi hi; ;
hi.Testing();
return 0;
}
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .
Please use code tags when posting code. I added them in the quote; see the difference?
In the Testing member function, all member variables of the Hi class are const, because the member function is const. That means you have a
Code:
Hello* const hello;
In other words, a const pointer to a (non-const) Hello. Hence, you can call non-const functions on the Hello instance, but you cannot modify the pointer.
Re: Non constant member function being called inside a constant member function
Thanks for editing the code .Its really looking good now . I understood something from your post .But one thing i want to know is we have not declared hello as constant (Hello *const hello ) inside Hi class , then how Testing member function will take it as constant pointer . OR is there any c++ concept on this topic which i am not aware of since i am very new to it .
Re: Non constant member function being called inside a constant member function
Quote:
Originally Posted by vikuseth
But one thing i want to know is we have not declared hello as constant (Hello *const hello ) inside Hi class , then how Testing member function will take it as constant pointer .
Because the object that owns the pointer is const in that context.
Re: Non constant member function being called inside a constant member function
Thanks . Now i got it .
whenever we are declaring some member function as constant all the members present inside that function scope will be taken as constant . Like here hello is a constant pointer . so if we do something like hello = new Hello() inside Testing() , then it will give one error (since we are changing the address). But here we are trying to access what hello points to (that is Test()) , So its not showing any error . Is my understanding correct ? Please correct if i am wrong .
Re: Non constant member function being called inside a constant member function
Quote:
Originally Posted by
vikuseth
Thanks . Now i got it .
whenever we are declaring some member function as constant all the members present inside that function scope will be taken as constant . Like here hello is a constant pointer .
I don't want to take away from your efforts to understand the const keyword but note there's a design alternative called immutability. You simply design your classes not to allow them being changed once created.