Non constant member function being called inside a constant member function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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 .
You're right, a constant member function cannot call any member functions that aren't constant, but Test is not Hi 's member function
But it is being called inside a constant member function . How it works ?
'hello' itself, as a pointer, is not being edited.
Its content is, but it isn't directly related to the Hi class.
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 .
Yeah, That's right, but, you shouldn't call that a 'constant pointer' but a 'variable pointer'.
Topic archived. No new replies allowed.