4 Replies - 240 Views - Last Post: 28 December 2012 - 12:45 PM Rate Topic: -----

#1 vikuseth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 14-August 12

Non constant member function being called inside a constant member fun

Posted 28 December 2012 - 07:41 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Non constant member function being called inside a constant member fun

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

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

Posted 28 December 2012 - 07:54 AM

Quote

As i know a non-constant member function cant be called inside a constant member function

Why not? As long as the non-constant member function doesn't alter the state of the class it can be called. Also in your sample program Hello::Test() should probably be declared const as well.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

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

Posted 28 December 2012 - 08:26 AM

There is also the 'mutable' keyword in C++ that will allow you to alter the state of a member variable within a const method if and only if that variable is declared mutable.
Was This Post Helpful? 0
  • +
  • -

#4 vikuseth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 14-August 12

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

Posted 28 December 2012 - 12:32 PM

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 .
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,233
  • Joined: 21-June 11

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

Posted 28 December 2012 - 12:45 PM

View Postvikuseth, on 28 December 2012 - 08:32 PM, said:

Is my understanding correct ? Please correct if i am wrong .


Yes, you've got it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1