LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2006, 05:38 AM   #1
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Rep: Reputation: 31
the meaning of "const" in C/C++ with functions


Hi, I have a simple(?) question about the type-qualifier "const" in use with functions declaration...

What does this mean?
Code:
const char function_a () {...
And what about this?
Code:
char function_b () const { ...
Thanks
 
Old 09-12-2006, 08:46 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

In C, the "const" prefix is for any type - and is used only to generate an error (or warning) if the value changes after an assignment.

E.g.
const char c;
c = 'a';
c = 'b';

It is mainly used for pointers, so the compiler can prevent mistakes like this
const char * function_a()
{
return "a";
}

char *ptr = function_a();
strcpy(ptr,"b");

The other use, is telling the compiler that the function is returning something based on its parameters only and the result will be the same every time you call it with the same parameters.

So for example the function:
int fact(int n) const
{
int i, sum;
for ( sum=1,i=2 ; i<=n ; i++ )
sum *= i;
return sum;
}

if you later use something like
int x = fact(32);
the compiler can optimize and there will be no loop executed in the program - it can calculate the number at compile time.

Not all compilers support this use of "const" though.
 
Old 09-12-2006, 09:54 AM   #3
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Also, in the second use (putting const after the function signature and before the function body), when used in a member function of a class, makes the function promise not to alter the object of the class, and this function can be called from a constant instance. Like this:

Code:
class cApplesauce
{
private:
   int m_iTheValue;

public:
   void setTheValue (int iNewValue)
   {
       m_iTheValue = iNewValue;
   }

   int getTheValue () const
   {
       return m_iTheValue;
   }
}
This example class just has set/get functions for m_iTheValue. Notice const after the getTheValue function.

Code:
cApplesauce theObject;
theObject.setTheValue (42);
cout << theObject.getTheValue () << endl;

const cApplesauce theConstObject = theObject;
theConstObject.setTheValue (69); // Not allowed!
cout << theConstObject.getTheValue () << endl;
This code snippet instantiates a regular object, sets its value, and prints it. It is okay to set the value.

Then it instantiates a const object (by copying the regular object (which will set its value to 42)), and attempts to set its value to 69. That won't work; the set function has not promised to change the value, and this is a const object. Then it prints the value, which works because the get function HAS promised not to change anything.

Thanks,
Mark
 
Old 09-13-2006, 04:40 AM   #4
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Original Poster
Rep: Reputation: 31
Wow... you guys really enlightened me... din know the simple const has so many meanings...

Thanks
 
Old 09-13-2006, 05:13 PM   #5
Abomb
Member
 
Registered: Dec 2005
Location: ~/
Distribution: Ubuntu
Posts: 156

Rep: Reputation: 30
Just to add a little note on using const with pointers, which is a good idea. You can define a pointer numerous ways:

Code:
char* ptr; // Non-const pointer.
const char* cptr; // Promise not to change the pointer itself.
char* const ptrc; // Promise not to change what the pointer is pointing to.
const char* const cptrc; // Promise not to change the pointer AND what it's pointing to.
This might not look like it's important but:

Code:
++ptr;    // legal.
++cptr;   // legal.
++ptrc;   // illegal.
++cptrc;  // illegal.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"GRUB _" when booting, nothing but "ctrl+alt+del" functions rabidpencil Linux - Newbie 15 08-08-2006 10:20 AM
Meaning of "const" ArthurHuang Programming 2 06-02-2006 04:34 AM
C/C++ functions similar to BASH's "cp", "mv", "mkdir", etc? kornerr Programming 10 04-23-2006 09:48 AM
learned "aliases", what are "functions" in .bashrc? learnfast Linux - Newbie 3 03-15-2005 04:24 AM
"ifcfg-ethx" and "network-functions" files peok Linux - Networking 12 08-13-2003 06:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration