Thread: const function

  1. #1
    Registered User
    Join Date
    Jun 2011
    Calcutta, India, India

    const function

    What is the difference between writing const before and after the function prototype?
    Last edited by neutron_star; 08-11-2011 at 06:04 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    You need to ask your questions more clearly. Your question is meaningless. A "function definition" has a specific meaning in C++, which I suspect you have misunderstood. Within that meaning, const before or after the function definition either equates to "code that will not compile" or "the const has no relationship to the function definition".

    If you have a particular meaning in mind of "before the function definition" and "after the function definition" you need to provide examples so we understand what you mean.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Calcutta, India, India
    sorry its function prototype.
    I have changed it.

  4. #4
    Join Date
    Dec 2007
    What part of
    you need to provide examples so we understand what you mean.
    do you fail to understand?

  5. #5
    Registered User
    Join Date
    Oct 2008
    I think he means something like this, two different member functions:
    class A {
    const std::string &someFunc();
    std::string &someOtherFunc() const;
    The difference is that "someFunc" returns a const reference to a string and someOtherFunc returns a non-const reference to a string. Another difference is that "someOtherFunc" doesn't let you modify the object for which this function is a member, and that "someFunc" can not be called on a const object.
    So: const after the function declaration means that it is possible to call the function when the object is const, though it makes sure you never change the object, at least not whatever you aren't allowed to change.

    For instance:
    A a;
    const A aConst;
    a.someFunc();  // Legal: call on non-const object to a function that doesn't take a const object.
    a.someOtherFunc();  // Legal: const function called on non-const object; this object will become const in someOtherFunc.
    aConst.someFunc();  // Illegal: someFunc() requires a non-const object, but a const object was passed.
    aConst.someOtherFunc();  // Legal: aConst is a const object, so it's fine to call a function making it const.
    So whenever you make a function that doesn't change the object, you should always make it const. That is completely unrelated to a const returned value, the following is perfectly legal and very common:
    const std::string &someFunction() const;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Const int not const in function
    By rogster001 in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2009, 04:54 AM
  2. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  3. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  4. function overloading. const T and const T* parameters
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-07-2006, 11:04 AM
  5. const on a function???
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2002, 06:07 AM