Use of 'const' in C++ functions

  • Context: C/C++ 
  • Thread starter Thread starter JesseC
  • Start date Start date
  • Tags Tags
    C++ Functions
Click For Summary
SUMMARY

The discussion clarifies the use of 'const' in C++ functions, specifically in the context of function parameters and member functions. When declaring a function with a parameter as 'class const & parameter', the 'const' ensures that the referenced object cannot be modified. Furthermore, in a member function declared as 'double functionName(class const & parameter) const', the first 'const' protects the parameter, while the second 'const' guarantees that the member function will not alter any member data of the class instance. This understanding is crucial for maintaining const correctness in C++ programming.

PREREQUISITES
  • Understanding of C++ syntax and semantics
  • Familiarity with references in C++
  • Knowledge of member functions in C++ classes
  • Concept of const correctness in C++
NEXT STEPS
  • Study C++ const correctness principles in depth
  • Explore the implications of const references in C++
  • Learn about the C++ compiler error messages related to const violations
  • Review best practices for using const in C++ member functions
USEFUL FOR

C++ developers, software engineers, and programmers seeking to enhance their understanding of const correctness and improve code reliability in C++ applications.

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!
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
89
Views
7K
  • · 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
3K