Thread: differences between const applications

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    34

    differences between const applications

    Hello guys,

    I would like to understand what's the difference between applications of const over functions , this means :
    Code:
    const function_name(data type input) ;
    const function_name(data type input) const;
    const function_name(void) const;
    I understand the first application of using const before function which it's:
    Code:
    const function_name(data type input)
    this means that function returns CONST and can't be changed by any else function.


    About the other two applications, could anyone please explain in simple words their applications?


    Much appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,578
    Well actually all three forms are invalid because of the leading const without a type.

    The last two are only valid with class member functions. They both inform the compiler that neither of those functions will modify the class state in any way.

    Also don't forget that you can also const qualify the parameters in any function to prevent modification of the parameter.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    34
    Quote Originally Posted by jimblumberg View Post
    Well actually all three forms are invalid because of the leading const without a type.

    The last two are only valid with class member functions. They both inform the compiler that neither of those functions will modify the class state in any way.

    Also don't forget that you can also const qualify the parameters in any function to prevent modification of the parameter.
    I understand the section that they are valid in class user defined ..
    may please explain by an example what do you mean "neither of those functions will modify the class state in any way" ? to modify what the input that the function called by? but if so, then what about
    const function_name(void) const that this form doesn't have input at all?

    Appreciated if you could example by simple code of what you mean.



    Appreciated.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,578
    What don't you understand?

    If you const qualify a class member function that function can't modify any class member variable. The kind and number of parameters don't matter.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    34
    Quote Originally Posted by jimblumberg View Post
    What don't you understand?

    If you const qualify a class member function that function can't modify any class member variable. The kind and number of parameters don't matter.
    I don't understand what do you mean in context of C++ programming.

    I mean , could you example what you elaborate by an example describe your explanation? appreciated much.

    what do you mean "if your class member function is const like this void functio_name(data type input) const then that function can't modify any class member variable" ? what do you mean by that? can't modify any class member variable of the same class or any class? thanks

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    34
    Any help? thanks alot.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,166
    Consider this class:
    Code:
    class X
    {
    public:
        int get()
        {
            return value;
        }
    
        void set(int n)
        {
            value = n;
        }
    private:
        int value = 0;
    };
    Now, suppose you declare an object of this class and use it:
    Code:
    X x;
    x.set(123);
    std::cout << x.get() << std::endl;
    No problem, great. But what if we have a function like this:
    Code:
    void foo(const X& x)
    {
        x.set(123);
        std::cout << x.get() << std::endl;
    }
    That we call like this:
    Code:
    X x;
    foo(x);
    Now, the compiler will complain, informing us that in the context of foo, x, being a const reference, cannot be modified. Okay, so we modify it before we call the function:
    Code:
    void foo(const X& x)
    {
        std::cout << x.get() << std::endl;
    }
    and:
    Code:
    X x;
    x.set(123);
    foo(x);
    But the compiler still complains that we're trying to modify x in a const context! The reason is that get, despite not modifying the member variable named value, remains a non-const member function, and so it could plausibly be changed to modify a member variable of the X object. Therefore, we should write:
    Code:
    class X
    {
    public:
        int get() const
        {
            return value;
        }
    
        void set(int n)
        {
            value = n;
        }
    private:
        int value = 0;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    928
    If you're comfortable with the relationship between pointers and const, and which pointer conversions are allowed, then you can apply that understanding to const member functions.

    Imagine that each member function is a free function that takes a pointer to the object instance as a first parameter, this:
    Code:
        struct Point2D {
            double x;
            double y;
    
            double r() const;
            void reflectOverYAxis();
        };
    
        // real C++
        double Point2D::r() const {
            return std::sqrt(x * x  +  y * y);
        }
    
        // also real C++
        double Point2D::r() const {
            return std::sqrt(this->x * this->x  +  this->y * this->y);
        }
    
        // still real C++
        double Point2D::r() const {
            const Point2D* instance = this;
            return std::sqrt(instance->x * instance->x  +  instance->y * instance->y);
        }
    
        // still real C++, but no longer a member function
        double r(const Point2D* instance) {
            return std::sqrt(instance->x * instance->x  +  instance->y * instance->y);
        }
    Compare that with an implementation of reflectOverYAxis, which being a non-const member function, would have a this pointer of type Point2D* instead of const Point2D*:

    Code:
        // real C++
        void Point2D::reflectOverYAxis() {
            x *= -1;
        }
    
        // also real C++
        void Point2D::reflectOverYAxis() {
            this->x *= -1;
        }
    
        // still real C++
        void Point2D::reflectOverYAxis() {
            Point2D* instance = this;
            instance->x *= -1;
        }
    
        // still real C++, but no longer a member function
        void reflectOverYAxis(Point2D* instance) {
            instance->x *= -1;
        }
    The difference is that in a const member function, this points to a const instance, while in a non-const member function this points to a non-const instance. You can't modify the members of a const instance (or any const aggregate object), so the result is that const member functions don't let you change data members.

    To laserlight's point, even if a non-const member function does not modify instance data, you still can't call that member function on a const instance, because you can't cast a const T* to a T*. It would be like trying to call the last version of reflectOverYAxis with a const Point2D*.
    Last edited by CodeMonkey; 12-08-2020 at 08:23 AM. Reason: courier
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Get Installed applications list and applications activity
    By arunarora in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2009, 09:41 AM
  4. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  5. Replies: 7
    Last Post: 04-28-2008, 09:20 AM

Tags for this Thread