C/C++ Use of 'const' in C++ functions

  • Thread starter Thread starter JesseC
  • Start date Start date
  • Tags Tags
    C++ Functions
Click For Summary
The discussion centers on the use of 'const' in C++ functions, clarifying its implications for parameter references and member functions. When a function is defined as double functionName(class const & parameter), the 'const' indicates that the reference to the class cannot be modified. This means that the object cannot be altered through this reference, nor can it be passed to another function that requires a non-const parameter. Additionally, if the function is a member function declared as double functionName(class const & parameter) const, the first 'const' still prevents modification of the parameter reference, while the second 'const' ensures that the member function itself will not modify any member data of the class instance it belongs to. Attempting to modify member data within such a function will result in a compiler error. The discussion emphasizes the importance of understanding "const correctness" in C++.
JesseC
Messages
247
Reaction score
2
The use of 'const' in functions seems to be an endless source of confusion. Am I right in thinking that in this case:

double functionName(class const & parameter) {...}

the const specifies that the reference to the class being fed into the function may not be modified?

And that in this case (where the function is a member function of a class):

double functionName(class const & parameter) const {...}

the two const's specify that the reference to the class being fed in may not be modified AND that any member data may also not be modified?
 
Technology news on Phys.org
JesseC said:
Am I right in thinking that in this case:

double functionName(class const & parameter) {...}

the const specifies that the reference to the class being fed into the function may not be modified?

No, it means that the class (object) may not be modified via that reference. You cannot substitute another object for it via an assignment statement, nor can you pass it to another function via a non-const parameter, nor can you invoke a member function which is not 'const'.

And that in this case (where the function is a member function of a class):

double functionName(class const & parameter) const {...}

the two const's specify that the reference to the class being fed in may not be modified AND that any member data may also not be modified?

The first 'const' has the same meaning as above. The second 'const' indicates that this member function will not modify the member data of the object that the member function is "attached" to. If you try to write the member function so that it does modify any member data, the compiler will give you an error message.

Here's a good guide to "const correctness":

http://www.parashift.com/c++-faq-lite/const-correctness.html
 
Last edited by a moderator:
Cheers!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
89
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K