-
December 28th, 2012, 09:40 AM
#1
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 .
-
December 28th, 2012, 10:24 AM
#2
Re: Non constant member function being called inside a constant member function
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 28th, 2012, 01:06 PM
#3
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 .
-
December 28th, 2012, 01:22 PM
#4
Re: Non constant member function being called inside a constant member function
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.
-
December 28th, 2012, 02:39 PM
#5
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 .
-
December 29th, 2012, 09:21 AM
#6
Re: Non constant member function being called inside a constant member function
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|