Thread: Constant Member function

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    Constant Member function

    • Defining as const, a member function that calls a non-const member function of the class on the same instance of the class



    What is the meaning of the above sentence? Can anybody please explain it with an example?

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    635
    What is the context? Is this sentence preceded by "The following are errors"?

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by christop View Post
    What is the context? Is this sentence preceded by "The following are errors"?
    • Following are compilation errors
      • Defining as const, a member function that modifies a data member of an object
      • Defining as const, a member function that calls a non-const member function of the class on the same instance of the class
      • Invoking a non-const member function on a const object

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    635
    Code:
    class CompilationError {
        void nonConstMethod();
        void constMethod() const { nonConstMethod(); } // can't do this
    };
    In this case, the "const" after "constMethod()" tells the compiler and all code that this method is guaranteed not to modify "this" instance of the class. "nonConstMethod()" does not make that guarantee.

    "constMethod()" cannot guarantee that "this" instance will not be modified by "nonConstMethod()", so it's a compilation error to call "nonConstMethod()" from "constMethod()".

    It's very similar to passing a pointer to const something to a function that takes a pointer to non-const something:

    Code:
    void mightModifyFoo(Foo *);
    void doesNotModifyFoo(const Foo *foo)
    {
        mightModifyFoo(foo); // can't do this
    }

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by christop View Post
    Code:
    class CompilationError {
        void nonConstMethod();
        void constMethod() const { nonConstMethod(); } // can't do this
    };
    In this case, the "const" after "constMethod()" tells the compiler and all code that this method is guaranteed not to modify "this" instance of the class. "nonConstMethod()" does not make that guarantee.

    "constMethod()" cannot guarantee that "this" instance will not be modified by "nonConstMethod()", so it's a compilation error to call "nonConstMethod()" from "constMethod()".

    It's very similar to passing a pointer to const something to a function that takes a pointer to non-const something:

    Code:
    void mightModifyFoo(Foo *);
    void doesNotModifyFoo(const Foo *foo)
    {
        mightModifyFoo(foo); // can't do this
    }
    Thank you Sir, I could get the point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing member variable as default arg to member function
    By Memloop in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2009, 06:49 PM
  2. Initializing constant object member of a class
    By Canadian0469 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2008, 08:05 PM
  3. Replies: 3
    Last Post: 07-23-2006, 01:09 PM
  4. Function Pointers to a Member, from a Member
    By littleweseth in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2003, 02:12 AM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM

Tags for this Thread