Thread: about constant functions and variables

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    about constant functions and variables

    Hi guys,

    I wanted to know that if we have a function like:

    const int GetRoll();

    Does this constant function returns a type "int" which is fixed?

    Is it necessary that with a constant function, the variable should also be constant?

    eg:

    const int RollNo;

    const int GetRollNo();

    Thanks

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    No, adding const to a function return value of a built-in type (i.e. int, float, etc.) serves no useful purpose at all. In fact, some compilers will give you a warning saying so.

  3. #3
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Try this:

    const return type
    -- reserved for when I come up with 1 --

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Note that adding const to a return value type of a non-built-in type may make sense if you want to prevent the user of a function from accidentally doing something silly like the following:


    Code:
    #include <string>
    
    std::string returnSomeString()
    {
        std::string result = "blah blah";
        // ...
    
        return result;
    }
    
    int main()
    {
        // This makes no sense! returnSomeString() returns a temporary that will
        // cease to exist right after the append() method invoked on it returns. Hence
        // there is no point in changing the temporary.
        returnSomeString().append( "meep meep!" );
    }
    The following would prevent that from even compiling.

    Code:
    #include <string>
    
    const std::string returnSomeString()
    {
        std::string result = "blah blah";
        // ...
    
        return result;
    }
    
    int main()
    {
        // error! this won't compile ... can't modify a constant object
        returnSomeString().append( "meep meep!" );
    }

  5. #5
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Quote Originally Posted by antred View Post
    The following would prevent that from even compiling.

    Code:
    #include <string>
    
    const std::string returnSomeString()
    {
        std::string result = "blah blah";
        // ...
    
        return result;
    }
    
    int main()
    {
        // error! this won't compile ... can't modify a constant object
        returnSomeString().append( "meep meep!" );
    }
    returnSomeString() still is temporary and hence even if you append it or you dont(w/o const RT), hardly makes any difference in the code. Right?
    -- reserved for when I come up with 1 --

  6. #6
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by R41D3N View Post
    returnSomeString() still is temporary and hence even if you append it or you dont(w/o const RT), hardly makes any difference in the code. Right?
    The point is that unlike the first example (without the const), which will compile and do the pointless append() without giving you any indication that you're doing something silly, the second example (with the const) won't compile at all, which should give you a pretty good hint that you might be doing something wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variables and Functions
    By Hitetsu in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 08:01 AM
  2. Replies: 9
    Last Post: 01-29-2006, 06:57 PM
  3. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  4. Replies: 2
    Last Post: 12-25-2001, 04:18 PM
  5. Functions and Variables used by them
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2001, 01:32 PM