Non constant member function being called inside a constant member function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Non constant member function being called inside a constant member function

  1. #1
    Join Date
    Jan 2012
    Posts
    17

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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Non constant member function being called inside a constant member function

    Quote Originally Posted by vikuseth View Post
    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

  3. #3
    Join Date
    Jan 2012
    Posts
    17

    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 .

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2012
    Posts
    17

    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 .

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Non constant member function being called inside a constant member function

    Quote Originally Posted by vikuseth View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center