What is meant with const at end of function declaration

0 votes

I received a book in which it is written:

class Foo 
{
public:
    int Bar(int random_arg) const
    {
        // code
    }
};

What does it mean?

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
623 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
A "const function," marked by the term const following a function declaration, causes a compiler error if this class function changes a class member variable.

However, accessing class variables within the function is permitted, but writing within the function will result in a compiler error.

This pointer is implicitly sent to the function.

So, a method like int Foo::Bar(int random arg) (without the const at the end) yields a function like int Foo Bar(Foo* this, int random arg) and a call like Foo f; f.

Internally, Bar(4) will be equivalent to Foo f; Foo Bar (&f, 4).

Adding the const at the end (int Foo::Bar(int random arg) const) makes the declaration with a const this pointer: int Foo Bar(const Foo* this, int random arg).

Because the type of this in this example is const, no changes to member variables are available.

It is feasible to relax the "const function" limitation that prevents the function from writing to any class variable.

These class variables are tagged with the term mutable to allow them to be editable even when the function is declared as a "const function."

Thus, if a class variable is designated as changeable and a "const function" writes to it, the code will compile correctly and the variable can be changed.

(C++11)

Changing the placement of the const keyword in a C++ statement has completely distinct semantics, as is typical when working with the const keyword.

The preceding const use only applies when const is added to the end of the function declaration after the parenthesis.
answered Aug 1, 2022 by Damon
• 4,960 points

edited Mar 5

Related Questions In C++

0 votes
0 answers

What is the point of function pointers?

The purpose of function pointers is difficult ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
468 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
660 views
0 votes
1 answer

What is the name of the "<<" and ">>" operators? [duplicate]

According to cplusplus.com's documentation: This operator (<<) applied to ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
486 views
0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
664 views
0 votes
0 answers

What is the meaning of "generic programming" in c++?

What does generic programming in C++ mean? I'm ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
704 views
0 votes
0 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
829 views
0 votes
0 answers

const int *p vs. int const *p - Is const after the type acceptable?

My coworker is 0 for 2 on questions inspired by him (1, 2), so I decided to give him a chance to catch up. Our most recent argument is about the placement of "const" on declarations. He believes it should be placed either in front of the type or after the pointer.  The explanation is that this is what everyone else does, and different styles are likely to be confusing.  Thus, a constant pointer to int and a constant pointer to int would be: const int *i; ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
660 views
0 votes
1 answer

What's the difference between constexpr and const?

Variables are protected from modification in your ...READ MORE

answered Aug 5, 2022 in C++ by Damon
• 4,960 points
1,613 views
0 votes
1 answer

How to implement constants in java

Here is the solution- public static final TYPE ...READ MORE

answered May 28, 2018 in Java by sophia
• 1,400 points
789 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
794 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP