Thread: Why use const in functions?

  1. #1
    Registered User
    Join Date
    Mar 2006

    Why use const in functions?

    I am reading something about C++, and it says "Methods that never change a class member value should be declared as constant using the const keyword in both the declaration and definition. This should be inserted just after the closing argument parenthesis and helps to prevent errors." Then, in its code example, is has
    class Dog
      void bark() const {cout << "WOOF!\n"; };
    I have never heard of this, nor even knew you could do such a thing, and thus obviously never do so in my code.

    Why would you do this? How does it prevent errors as the quote claims? Does anyone else do it? It just seems like silly, extra baggage to me.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    I beleive it's for const classes because const classes cannote use non-const functions.

    Or something like that, I was reading it not too long ago and read someting about const classes not being able to do certain things with nonconst functions.

  3. #3
    Registered User
    Join Date
    Mar 2006
    It appears the author is using just a normal class, nothing constant, and from the quote I mentioned it looks to me that his only reasoning was "It helps prevent errors."

    In fact, in an earlier code snippet the author had that very same function without the const keyword. Now he just comes out and says it should be there and claims that it somehow prevents errors.

    The only thing I can think up is that maybe it will help prevent you from accidentally changing a variable where you didn't mean to since, as my test program I just made for this has just showed me, the compiler gives errors when I try to change any of the object's variables within one of its functions that is constant. But if that's the only reason, it just makes me wonder how high that guy must have been if he accidently changed one of the Dog's variables in that bark function.

  4. #4
    Registered User
    Join Date
    Jan 2005
    It's used for const objects or references, not const classes. For example, when you pass an instance of a class to a function and that class is expensive to copy, it is common to pass a const reference instead. The function uses const as a promise not to change the state of the object passed to it. But it usually still needs to call functions that don't affect the state. Making the class's member functions const that don't change the state is a way for the class to make known which functions can be called without changing the state of the object.

    In some cases using const might make it easier for the compiler to optimize code. In general, it is used more as a contract between different parts of code and a way to enforce via the compiler what code will change and object's state and what won't.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    The whole concept is called const-correctness.
    Basically, if you have a const reference to something:
    const MyClass &something = get_a_reference();
    then you can only call const functions on that object.
    Now, const references are important. They are a promise not to change the values (both to the caller of a function that takes a const reference parameter, and to the optimizer, who can make assumptions), and the compiler will enforce this restriction. That's why it prevents errors: changing values when the user doesn't expect them to change is a baaad idea.
    Also, you cannot bind temporary objects to non-const references, except in some special circumstances. This becomes very important when you write templates and need to decide on a parameter type.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    If member function is not supposed to change things it's good idea to use it as const. This way you force compiler to protect you if you inadventertly try to change something:
    #include <iostream>
    using namespace std;
    class Int
    	int x;
    	Int(int s):x(s){}
    	void show()const
    		cout << x;
    		x = 10;//put this under comment and try to compile 
    int main()
    	Int test(10);;
    	return 0;
    I think this code will clearify things a little!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Class functions can't use const =/
    By neandrake in forum C++ Programming
    Replies: 10
    Last Post: 12-02-2004, 01:48 AM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM