#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 .

Constness of a member function only applies to the direct state of the object for its class, so it's completely irrelevant if the non-const hello calls a member function that changes its own state and not the state of hi. It would be different altogether if you tried to change where hello points because that's hi's direct state. It would also be different if hello itself were const, because Test() isn't const qualified. Finally, it would be different if hello were not a pointer because then the state of hello would be a subset of hi's state.

Edited 3 Years Ago by deceptikon

Sorry i am not getting you . I am new to c++ . so can you please explain something more on it ?

I think getting more into it would just confuse you further. Const correctness can be tricky, so it's probably best to just accept this example as an exception to the rule, handle errors as they arise, and wait for a little more experience to faciliate that "aha!" moment.

I think I can provide a bit more explanation. A pointer (e.g., hello) is just an ordinary variable (like an integer) that stores the address of an object. That value (the address) can be changed, like any other variable value, unless it is const. Here are a few cases:

Hello* p1;       // this is a non-const pointer to a non-const Hello object.
Hello* const p2; // this is a const pointer to a non-const Hello object.
const Hello* p3; // this is a non-const pointer to a const Hello object.

The point here is that your data member hello is declared in the same way as p1, and when inside a const member function, that data member becomes as if it was declared in the same way as p2, meaning that you cannot change the value of hello (i.e., the address that it stores), but you can affect changes to the object that it points to (i.e., as in the above, p2 still points to a non-const Hello object).

What deceptikon tried to explain is that within a const member function, all the data members become const, but only at the "first level", so to speak. An integer value becomes a const integer value. A std::string object becomes a const std::string object. A pointer becomes a const pointer. A reference remains unchanged, because references are inherently const already. But in both of the last cases, the pointee (or referee) remains of the same constness.

very well explanation .
"within a const member function, all the data members become const, but only at the "first level"
I am not able to understand the above line stated by you . can you please help me in it ?

Things like pointers and references can be called "indirections" because they are used to redirect you to another object or variable, i.e., the "pointee" or the "referee". So, when I said that all the data members become const, but only at the "first level", I really mean that the constness does not affect the target of an indirection, only the indirection itself.

I don't know of any way to explain this in clearer terms than that.

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 .
Thanks mike and deceptikon for taking me to the right path . I always get help from this forum . Thas why i really love this forum.

This question has already been answered. Start a new discussion instead.