Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:11:54+00:00 2026-06-17T06:11:54+00:00

Possible Duplicate: is const (c++) optional? Constant Member Functions I have seen different posts

  • 0

Possible Duplicate:
is const (c++) optional?
Constant Member Functions

I have seen different posts around about int a() const. My question is exactly why do we want to put the const keyword there? I know that it prevents us from making changes on members of the class the function is in, but so what?

We could just write a comment above the function and tell the coder not to make any changes to the class itself inside the function, but why do we really care about the const after the void declaration? In which scenarios may there be a better way to use for example int a() const instead of just int a() and does the compiled code change at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:11:56+00:00Added an answer on June 17, 2026 at 6:11 am

    It does make a difference. Consider A to be a class with a print() method. Then this:

    const A a;
    a.print();
    

    only compiles is print is defined as const.

    Of course, the main reason is to prevent any changes to the object inside the class. The

    We could just write a comment above the function and tell the coder not to make any changes to the class itself inside the function

    is just wishful thinking. This doesn’t really happen. So if someone doesn’t read the comment (or does and ignores it), you’ll get compiler errors.

    There’s also the benefit of optmization – the compiler can better parallelize tasks on const objects because it can assume they don’t change between operations on them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ static constant string (class member) static const C++ class member initialized
Possible Duplicate: Modified a constant in c const int z = 420; const void
Possible Duplicate: What is the difference between const and readonly? i have doubt in
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
Possible Duplicate: what is the difference between const int*, const int * const, int
Possible Duplicate: what is the difference between const int*, const int * const, int
Possible Duplicate: what is the difference between const int*, const int * const, int
Possible Duplicate: const in C vs const in C++ I have following code In
Possible Duplicate: static const vs #define In C++, to define a constant to be
Possible Duplicate: Should I return const objects? (The original title of that question was:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.