Thread: const's position

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    const's position

    howdy~

    i got confused with const when they appear before or after the function head when declare a function, just like this below:
    Code:
    const int func1(const int x)
    {
    	int temp = x + 1;
    	return temp;
    }
    int func2(const int x) const
    {
    	int temp = x + 1;
    	return temp;
    }
    what about the difference between them ?
    Never end on learning~

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The former returns a constant int. The latter const means that func2 cannot change the object that calls it.
    So, if const is before the function, that means the function is returning a constant. If const is after the function, that means the function cannot change the object that calls it.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx for the quick reply~

    with your remarks since const after the function means the function itself cannot change the object that calls it, this format should be applied only for class member function right ? and is it possible for us to declare a member function with both consts before and after ? just like this:
    Code:
    class Test
    {
        public:
        const int func(int x) const;    // Is this possible ?
    }
    Never end on learning~

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yes.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Yes, const after a function is only for member functions of classes. And yes, you could have const both before and after. Doing so means that the function returns a constant and cannot change the object that calls it.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    const int Func1();
    int Func2();
    
    //Later...
    int a=Func1();
    int b=Func2();
    
    a+=5;
    Is that code valid? What I am getting at is, I don't see how the preceding 'const' could affect anything.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Yes, const after a function is only for member functions of classes. And yes, you could have const both before and after. Doing so means that the function returns a constant and cannot change the object that calls it.
    thanx !!!
    Never end on learning~

  8. #8
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Quote Originally Posted by bennyandthejets
    Is that code valid? What I am getting at is, I don't see how the preceding 'const' could affect anything.
    In case the return value of a funtion is declared as const, it can't be used as an l-value. So, the code below won't compile :
    Code:
    // return value declared as const
    const int getVal();
    ...
    if( getVal() == 1 ) // OK, return value used as r-value
    {
    // do something
    }
    getVal() = 1; // compiler error, the return value is const
    Regarding const functions (member functions not allowed to modify the caller object) you should also inspect the mutable keyword.

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What about this?
    Code:
    int getVal();
    
    //Later...
    getVal()=1;
    How does that differ from if the function returned a const int? You can't assign a value to a function in that way.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Quote Originally Posted by bennyandthejets
    What about this?
    How does that differ from if the function returned a const int? You can't assign a value to a function in that way.
    You (can) never assign values to a function, rather to the object returned by the function. The return values can be temporary objects (if returned by value), however, you can also return references or pointers to objects, through which you can access e.g. a class's members.

    I'd have some remarks:
    1.) returning references or pointers to local objects - which are destroyed after exiting the function's scope - is a very bad idea, as you're pointing to something that has been already destroyed

    2.) member functions which return references to members less accessible then themselves is not an error, but should be applied only when really wanted and needed

    The following example is a very simply one, and does not respect the above mentioned poit 2.) It's just to show the idea of l_value and r_value:
    Code:
    class Dummy
    {
    private:
    	int i;
    public:
    	Dummy( int i_in ) : i(i_in) {};
    // function returning const reference to private member, can only be used to read the value
    	const int& getRVal() 
    	{
    		return i;
    	}
    
    /* function returning reference to private member, through which we can alter the private member (not a good practice, only for example)*/
    	int& getLVal() 
    	{
    		return i;
    	}
    
    };
    
    int main()
    {
    	Dummy dumObj( 1 );
    	//++dumObj.getRVal();	// won't compile, operator ++ needs an l_value!
    	cout << ++dumObj.getLVal() << endl;
    
    return 0;
    }
    [modified - added more detailed explanation]
    Last edited by Carlos; 05-26-2004 at 01:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM